精品网站建设费用,重庆市两江新区建设管理局网站,绿色门业宽屏网站模板 破解,软件研发和开发的区别大家好#xff0c;我是空空star#xff0c;本篇带大家了解一道简单的力扣sql练习题。 文章目录前言一、题目#xff1a;183. 从不订购的客户二、解题1.正确示范①提交SQL运行结果2.正确示范②提交SQL运行结果3.正确示范③提交SQL运行结果4.正确示范④提交SQL运行结果总结前言… 大家好我是空空star本篇带大家了解一道简单的力扣sql练习题。 文章目录前言一、题目183. 从不订购的客户二、解题1.正确示范①提交SQL运行结果2.正确示范②提交SQL运行结果3.正确示范③提交SQL运行结果4.正确示范④提交SQL运行结果总结前言 一、题目183. 从不订购的客户
某网站包含两个表Customers 表和 Orders 表。编写一个 SQL 查询找出所有从不订购任何东西的客户。
Customers 表
-----------
| Id | Name |
-----------
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
-----------
Orders 表
----------------
| Id | CustomerId |
----------------
| 1 | 3 |
| 2 | 1 |
----------------
例如给定上述表格你的查询应返回
-----------
| Customers |
-----------
| Henry |
| Max |
-----------
二、解题
1.正确示范①
提交SQL
select Name Customers
from Customers
where id not in(select CustomerId from Orders
)运行结果 2.正确示范②
提交SQL
select Name Customers
from Customers u1
left join Orders u2
on u1.idu2.CustomerId
where u2.CustomerId is null运行结果 3.正确示范③
提交SQL
select Name Customers
from Orders u1
right join Customers u2
on u2.idu1.CustomerId
where u1.CustomerId is null
运行结果 4.正确示范④
提交SQL
select Name Customers
from Customers u1
left join Orders u2
on u1.idu2.CustomerId
group by u1.id,Name
having count(CustomerId)0运行结果 总结 正确示范①思路 使用 not in 查询不在客户订单列表中的客户 正确示范②思路 使用 left join 将客户和订单表关联起来最后用where限定关联后的u2的CustomerId是null 正确示范③思路 使用 right join 将客户和订单表关联起来最后用where限定关联后的u2的CustomerId是null 正确示范④ 思路 使用 group by xx having 语句取count(CustomerId)0的。