Tabel_A
---------
id name
1 Kursi
2 Roda
3 Gigi
Tabel_B
---------
id id_tabel_A
1 2
Result
--------
name Status
Kursi 0
Roda 1
Gigi 0查询结果:…?
发布于 2019-09-24 14:59:40
使用left join和case when
select name, case when b.id_tabel_A is null then 0 else 1 end as status
from tableA a left join tableB b on a.id=b.id_tabel_A发布于 2019-09-24 15:04:34
应用IF语法
SELECT a.name,
IF(
(
SELECT count(b.id_tabel_A)
from Tabel_B as b
WHERE b.id_tabel_A = a.id -- connect
) > 0
, "YES", "NO") as status
from Tabel_A as a发布于 2019-09-24 19:25:59
我推荐使用exists
select a.name,
(exists (select 1
from tableB b
where a.id = b.id_tabel_A
)
) as status
from tableA a; 我更喜欢exists的原因是它可以自动处理tableB中的重复项。您不必担心查询返回重复的结果。
https://stackoverflow.com/questions/58074647
复制相似问题