我有两张桌子:
Table A
Column: username
joe
mike
Table B
Column: username
joe
joe
bob
bob
bob
bob
mike
mike
我只想查看表 B 中的用户名,而不是表 A。什么是只会给我用户名的查询bob
?
顺便说一句,表 B 可以有多个相同的用户名,而表 A 不能,所以我假设我需要使用GROUP BY
?
SELECT DISTINCT table2.username
FROM table1 RIGHT JOIN table2 ON table1.username = table2.username
WHERE table1.username IS NOT NULL
select distinct username
from b
where username not in (select username from a)
你也可以使用连接
select distinct username
from b
left join a on a.username = b.username
where a.username is null
这个怎么样
SELECT * FROM tableb WHERE NOT EXISTS
(SELECT * FROM tablea WHERE tableb.username = tablea.username)
这应该工作
table_a 中的用户不在 b 中
SELECT username FROM table_a a WHERE NOT EXISTS (SELECT * FROM table_b b WHERE b.username = a.username);
table_b 中的用户不在 a
SELECT username FROM table_b b
WHERE NOT EXISTS (SELECT * FROM table_a a
WHERE b.username = a.username);