两个表之间的SQLite CASE
我有2个表格“主题”和“子”
表格主题:
+------------+-------------+
| id_topic | topic_name |
+------------+-------------+
| 1 | books |
| 2 | food |
| 3 | movies |
| 4 | weather |
| 5 | travel |
| 6 | pets |
+------------+-------------+
TABLE SUB:
+------------+
| id_topic |
+------------+
| 2 |
| 5 |
+------------+
我要打印这个:
+-------------+-------------+-------------+
| id_topic | topic_name | subscribed |
+-------------+-------------+-------------+
| 1 | books | no |
| 2 | food | yes |
| 3 | movies | no |
| 4 | weather | no |
| 5 | travel | yes |
| 6 | pets | no |
+-------------+-------------+-------------+
答案 0 :(得分:0)
您需要一个left join
和一个CASE语句:
select
t.*,
case
when s.id_topic is null then 'no'
else 'yes'
end subscribed
from topics t left join sub s
on s.id_topic = t.id_topic