SQL每日一题(20230814)

185 字
1 分钟
SQL每日一题(20230814)

SQL每日一题(20230814)#

题目#

有如下两张表G0227A(客户表)

IdName
1曹操
2关于
3刘备
4张飞

G0227B(订单表)

IdCustomerId
13
21

查询G0227B表(订单表)中找出从来没有买过商品的用户。

预计结果如下:

IdName
2关于

要求:用至少四种方法求解。#

create table G0814A
(
Id int,
Name varchar(20)
)
insert into G0814A values (1,'曹操');
insert into G0814A values (2,'关羽');
insert into G0814A values (3,'刘备');
insert into G0814A values (4,'张飞');
create table G0814B(
Id int,
CustomerId int
)
insert into G0814B values (1,3);
insert into G0814B values (2,1);

参考答案#

-- 方法一: 关联查询
SELECT a.* FROM G0814A aLEFT JOIN G0814B b ON b.customerid=a.idWHERE b.customerid IS NULL;
-- 方法二:不存在 not exists
select *from G0814A awhere not exists(select * from G0814B bwhere a.id = b.customerid;
-- 方法三:不包含 not in
selectfrom G0814A awhere a.id not inselect b.customerid from G0814B b);
-- 方法四: 差集
select * from G0814Awhere id inselect a.id from G0814A aexceptselect b.customerid from G0814B b
);

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!

打赏
SQL每日一题(20230814)
https://fanrich.eu.org/posts/程技/sql/sql-daily-question--20230814/
作者
richfan
发布于
2023-08-01
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
richfan
Hello, I'm richfan.
公告
欢迎来到我的博客!这是一则示例公告。
音乐
封面

音乐

暂未播放

0:000:00
暂无歌词
分类
标签
站点统计
文章
779
分类
8
标签
138
总字数
3,267,679
运行时长
0
最后活动
0 天前
站点信息
构建平台
Cloudflare Pages
博客版本
Firefly v6.13.5
文章许可
CC BY-NC-SA 4.0

文章目录