mysql使用内连接获取_MySQL内连接和外连接
发布日期:2021-06-24 13:28:14 浏览次数:2 分类:技术文章

本文共 782 字,大约阅读时间需要 2 分钟。

INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。

LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。

RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。

MySQL内连接(inner join on)

MySQL的内连接使用inner join on,它的效果跟使用where是一样的,如果联结的是两个表,那么需要左右的条件或者说字段是需要完全匹配的。

来看个例子:有两张表customers客户表和orders订单表,外键是cust_id,我们需要知道哪些客户有订单

select customers.cust_id,orders.order_num from customers , orders where customers.cust_id = orders.cust_id;

如果我们使用内连接的话就可以这样写:

select customers.cust_id,orders.order_num from customers inner join orders on customers.cust_id = orders.cust_id;

但是如果我除了这些有有客户的订单,我还想拿到所有的订单信息,那么怎么办呢?

MySQL外连接(left,right)

select customers.cust_id,orders.order_num from customers right outer join orders on customers.cust_id = orders.cust_id;

外连接包含左右连接,

左连接的结果是除了匹配条件的数据还包含左边表中的所有数据

右连接的结果是除了匹配条件的数据还包含右边表中的所有数据

转载地址:https://blog.csdn.net/weixin_33178459/article/details/113332976 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:php配置mysql文件怎么打开_配置PHP平台,MySQL打开不了
下一篇:sql server2014 怎么隐藏 触发器_《我的世界》居然用河豚制作隐藏门?工具豚表示:打工都是豚上豚...

发表评论

最新留言

很好
[***.229.124.182]2024年04月12日 21时27分56秒