我有三个表是这样的: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结束为状态。你最终应该得到一个布尔值 –