我正在尝试从表A中选择当前用户表B中不存在的所有记录
事实上,我有一个项目从表A获取横幅并向用户显示,然后我将此活动插入表B(参见日志)我不喜欢向用户显示重复横幅。
表A(横幅):
+-----------+
| bannerKey |
+-----------+
| x7y3 |
| r2s4 |
| j6n2 |
+-----------+
表B(见日志):
+-----------++----------+
| bannerKey || userName |
+-----------++----------+
| x7y3 || jack |
| j6n2 || Chris |
| r2s4 || Nicola |
| j6n2 || Allen |
| j6n2 || Nicola |
+-----------++----------+
那么,我怎样才能从当前用户那里获得当前用户的A表记录呢?过去没有看过这篇文章?
答案 0 :(得分:0)
你可以使用not in
select *
from table_a
were bannerKey not in ( select
bannerKey from table_b)
或加入
select *
from table_b
left join table_b a.bannerKey = b.bannerKey
where a.bannerKey is null
答案 1 :(得分:0)
试试这个:
select *
from banners a
where not exists (
select 1
from table2 b
where username = 'currentusername' -- substitute username here
and a.bannerKey = b.bannerKey
)