将会对这个问题表示感谢:
A (userid) B(userid)
1 2
2 5
3 7
4
5
6
7
给定表A(用户ID)和表B(用户ID),我们必须编写一个查询,其结果集是两个字段(1)所有A用户和(2)这些用户是否存在于B
查询#1(下面)是否有效?如果没有,请解释为什么它不会工作。
Resultset:
userid is_in_a
1 0
2 1
3 0
4 0
5 1
6 0
7 0
Select A.id,
case when exists ((select B.id from B where B.id=A.id)
then 1 else 0) as is_in_A
from A
Select userid, case userid
(when IS NULL then 0
else 1) as is_in_A
from
(Select userid
from A left outer join B on a.userid = B.userid)
由于
答案 0 :(得分:0)
我认为你在寻找:
Select A.id,
(case when exists (select 1from B where B.id = A.id) then 1 else 0 end) as is_in_A
from A;
您的查询有不必要的括号,但却遗漏了end
的{{1}}。
答案 1 :(得分:0)
此查询应该有效:
select A.userid, case when B.userid is null then 0 else 1 end as is_in_a
from A
left join B on B.userid = A.userid
Order By A.userid