码迷,mamicode.com
首页 > 数据库 > 详细

mysql 练习

时间:2018-08-06 10:25:44      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:HERE   customers   查询   pre   sam   客户   left join   tom   mys   

1 从不订购的客户

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Customers 表:

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+

Orders 表:

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

例如给定上述表格,你的查询应返回:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+
解答① 联接
select c.name as customers from customers c left join orders o on c.id = o.customerid where customerid is null
解答② not in
select c.name as customers from customers c where c.id not in (select customerid from orders)

mysql 练习

标签:HERE   customers   查询   pre   sam   客户   left join   tom   mys   

原文地址:https://www.cnblogs.com/islvgb/p/9428650.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!