我想从表1中查询名称,还要查找表2中是否存在名称。我有以下查询,但它似乎无法正常工作。有什么建议我做错了吗?
select A.name,
CASE WHEN A.name in (select B.name in table2 B)
THEN 'common'
ELSE 'not common'
END
from table1 A
Run Code Online (Sandbox Code Playgroud)
请注意,我必须从select子句本身获取“ common” /“ uncommon”。我正在使用postgres。
我将使用EXIST代替IN:
select
A.name,
CASE
WHEN EXISTS (select *
from table2 B
where B.name = A.name)
THEN 'common'
ELSE 'not common'
END
from
table1 A
Run Code Online (Sandbox Code Playgroud)
在 SELECT CASE 中使用子查询会花费更多。使用左连接代替如下所示
select A.name,
CASE WHEN B.name IS NOT NULL
THEN 'common'
ELSE 'not common'
END
from table1 A
left join table2 B
on A.name = B.name
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10683 次 |
| 最近记录: |