mysql 字段值1 2 3 如何查詢3是否存在

2021-10-19 00:23:13 字數 1061 閱讀 4392

我有乙個表(命名單位),結構如下:id type type_id name parent hide

12 child 2 no1-r 36 0

32 child 2 no2-l 0 0

36 parent 1 no1 0 0

42 parent 1 no4 0 0

59 child 2 no5-t 0 0

60 child 2 no6-r 72 0

63 child 2 no6-l 72 0

72 parent 1 no6 0 0

81 parent 1 no7 0 0

94 parent 1 no8 0 0

95 parent 1 no9 0 0

97 child 2 no9-r 95 0

99 child 2 no9-t 95 0

我要做的是遍歷所有id,如果id在parent中不存在並且type_id=1,則將hide設定為1。所以我的輸出表的結果是:id type type_id name parent hide

12 child 2 no1-r 36 0

32 child 2 no2-l 0 0

36 parent 1 no1 0 0

42 parent 1 no4 0 1

59 child 2 no5-t 0 0

60 child 2 no6-r 72 0

63 child 2 no6-l 72 0

72 parent 1 no6 0 0

81 parent 1 no7 0 1

94 parent 1 no8 0 1

95 parent 1 no9 0 0

97 child 2 no9-r 95 0

99 child 2 no9-t 95 0

所以基本上,因為id42,81和94在父級中不存在,所以它們的hide值被設定為1。然而,id36在父級中確實存在,因此不受影響,而且id32也不受影響,即使它在父級中不存在,因為它的type_id是2。

我無論如何也不能考慮在這個表上執行什麼mysql(執行版本5.6.44)更新來實現這個輸出,所以任何幫助都將非常感激!

mysql查詢表字段預設值

mysql查詢表字段預設值。假設表user有個字段 login times int 11 unsigned not null default 0 comment 登入次數 需要獲取該字段的預設值,實現方法 desc user 查詢結果 field type null default id varch...

mysql選擇字段 mysql欄位型別如何選擇

本文主要和大家分享mysql欄位型別如何選擇。希望能幫助到大家。盡量少的佔據儲存空間 整型 時間型別 選擇準確的型別 型別含義time 時分秒 datetime 年月日 時分秒 year 年份 date 年月日 timestamp 時間戳 1970 1 1到現在經歷的秒數 資料的整合最好固定長度 c...

mysql查詢多欄位 mysql多欄位模糊查詢

在最近的乙個專案需要實現在mysql單錶多關鍵字模糊查詢,但這數個關鍵字並不一定都存在於某個字段。例如現有table表,其中有title,tag,description三個字段,分別記錄一條資料的標題,標籤和介紹。然後根據使用者輸入的查詢請求,將輸入的字串通過空格分割為多個關鍵字,再在這三個欄位中查...