SQL每日一题(20230814)
185 字
1 分钟
SQL每日一题(20230814)
SQL每日一题(20230814)
题目
有如下两张表G0227A(客户表)
| Id | Name |
|---|---|
| 1 | 曹操 |
| 2 | 关于 |
| 3 | 刘备 |
| 4 | 张飞 |
G0227B(订单表)
| Id | CustomerId |
|---|---|
| 1 | 3 |
| 2 | 1 |
查询G0227B表(订单表)中找出从来没有买过商品的用户。
预计结果如下:
| Id | Name |
|---|---|
| 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 existsselect *from G0814A awhere not exists(select * from G0814B bwhere a.id = b.customerid;
-- 方法三:不包含 not inselect¥from 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/相关文章智能推荐
1
SQL每日一题F1021,while循环操作
程技2023-08-01
2
SQL每日一题F1025,复杂逻辑处理
程技2023-08-01
3
SQL每日一题F1028,关联子查询
程技2023-08-01
4
SQL每日一题F0215,多种方法及思路讲解
程技2023-08-01
5
MySQL常用30种SQL查询语句优化方法
程技2023-08-01
随机文章随机推荐
樊笼










