题解 | #检索每个顾客的名称和所有的订单号(二)#

检索每个顾客的名称和所有的订单号(二)

http://www.nowcoder.com/practice/4e73c4e770b941c9abc60814601ed498

搞懂各种连接很重要

本题思路

  • 关键信息:没有下过单顾客名也需要列出来,需要使用左外连接或者右外连接;最后采用order by排序;
  • 左外连接:连接符号左边的表里面的数据全部显示,连接符号右边的表如果与左表匹配则正常列出;如果不匹配则置为NULL;MySQL中为表1 left outer join 表2 on 连接条件,其中outer可以省略;
  • 右外连接:与左外连接相反;MySQL中为表1 right outer join 表2 on 连接条件,其中outer可以省略;
  • 各种连接区别博客:图解连接

右外连接代码

select c.cust_name cust_name, o.order_num order_num
from Orders as o right join Customers c on o.cust_id=c.cust_id
order by cust_name;

左外连接代码

select c.cust_name cust_name, o.order_num order_num
from Customers c left join Orders o on o.cust_id=c.cust_id
order by cust_name;
  • 注意:从group by开始就可以使用select里面的别名,所以order by也可以。
全部评论

相关推荐

07-11 13:16
湖南工学院 Java
坚定的芭乐反对画饼_...:谁也不知道,毕竟现在的互联网和十年前已经完全不同了,谁都无法预测未来
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 12:20
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务