MySQL连接:内连接、左外连接、右外连接

本文详细介绍了数据库查询中的内连接(inner join)、左外连接(left outer join)和右外连接(right outer join)的概念及使用方法,通过实例展示了不同连接类型在SQL语句中的应用,帮助理解如何在多个表之间进行数据关联和筛选。
摘要由CSDN通过智能技术生成

内连接(inner join)

在这里插入图片描述

语句:
	select [表字段1],[表字段2],... from [表A],[表B] where [表A.相同字段] = [表B.相同字段]
隐式内连接:
select first_name, last_name, order_date, order_amount
from customers, orders 
where customers.customer_id = orders.customer_id
显示内连接:
select first_name, last_name, order_date, order_amount
from customers c
inner join orders o
on c.customer_id = o.customer_id

左外连接(left outer join,outer可省略)

在这里插入图片描述

语句:
	select [表字段1],[表字段2],... from [表A] LEFT JOIN [表B] on [表A.预关联的字段] = [表B.预关联的字段]
select first_name, last_name, order_date, order_amount
from customers c
left join orders o
on c.customer_id = o.customer_id
SELECT
  o.`id`,
  o.`oil_name`,
  o.`address`,
  o.`longitude`,
  o.`latitude`,
  o.`discount`,
  o.`explains`,
  DATE_FORMAT (o.`adddate`, '%Y-%m-%d %H:%i') ADDDATE,
  o.`status`,
  a.id aid,
  a.`attMiniUrl`,
  a.`attType`,
  a.`attUrl`,
  a.`businessType`,
  a.`isdefault`,
  a.`name`,
  DATE_FORMAT (
    a.`uploadTime`,
    '%Y-%m-%d %h:%i'
  ) uploadTime
FROM
  b_oil o
  LEFT JOIN b_attachment a
    ON o.id = a.businessId
    AND a.businessType = 1 
<where>
    o.isdel=0
       <if test="oil_name !=null and oil_name !='' ">
          and o.oil_name like concat('%',#{oil_name},'%')
       </if>
       <if test="address !=null and address !='' ">
          and o.address like concat('%',#{address},'%')
       </if>
       <if test="status!=null and status!=''">
          and o.status=#{status}
       </if>
</where>
    <if test="begin!=null">
       limit #{begin},#{pageSize}
    </if>

右外连接(right outer join,outer可省略

在这里插入图片描述

select first_name, last_name, order_date, order_amount
from customers c
right join orders o
on c.customer_id = o.customer_id
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jsonCC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值