我有三個表是這樣的:SQL兩個左聯接和CASE statment
標籤
id name scope
----------------
6 abc 12
7 foo 12
8 bar 12
建立
id name scope_id
------------------
1 test1 12
2 test2 12
3 test3 12
build_tags
id tags_id build_id
---------------------
1 6 2
2 7 1
3 8 1
如何構造一個SQL查詢來顯示爲每個標籤和特定的build,則存在相關標籤(build_tags)?所以結果是:
id name presence
------------------
6 abc 0
7 foo 1
8 bar 1
現在我有這個查詢返回重複標記,因爲在每個標記而不是唯一的兩個記錄。我試着增加一個group by
但導致不正確的case
值。
select t.id, t.name,
case t.id when tags_id then true else false end as 'presence'
from tags t left join
builds b
on t.scope = b.scope_id and t.scope = 12 inner join
build_tags bt
on bt.build_id = b.id and b.id = 1
仍然給了我兩行每個標籤。對於在build_tags表中有記錄的兩個標記,一行是presence = true,一個存在= false – greener
哦,嘿,你可以嘗試類似case的情況,當t.id =「你的號碼」然後1結束爲狀態。你最終應該得到一個布爾值 –