左连接右连接 动态sql

sql进阶。
基本定义:

  left join (左连接):返回包括左表中的所有记录和右表中连接字段相等的记录。

  right join (右连接):返回包括右表中的所有记录和左表中连接字段相等的记录。

  inner join (等值连接):只返回两个表中连接字段相等的行。

  full join (全外连接):返回左右表中所有的记录和左右表中连接字段相等的记录。

现在呢举个例子:

  A表          

  id  name  

  1  小王

  2  小李

  3  小刘

  B表

  id  A_id  job

  1  2    老师

  2  4    程序员

  select a.name,b.job from A a inner join B b on a.id=b.A_id

  只能得到一条记录

  小李  老师

  select a.name,b.job from A a left join B b on a.id=b.A_id

  三条记录

  小王  null

  小李  老师

  小刘  null

  select a.name,b.job from A a right join B b on a.id=b.A_id

  两条记录

  小李  老师

  null  程序员

  select a.name,b.job from A a full join B b on a.id=b.A_id

  四条数据

  小王  null

  小李  老师

  小刘  null

  null  程序员

  以上的具体用法就看你的业务需求了,比如查询多有人的职业,没有职业的设置为null,左连接无疑是最

正确的,再比如查询所有职业对于的人,没有所对应的人就设置为0.右连接更为正确。

  当然在工作的我们会看到关联好几张表的情况,这时候我们会多写几个join on语句,具体是哪个连接要按

具体的业务而定。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 库存映射 -->
<mapper namespace="com.hebg3.mobiledealer.modules.client.store.order.dao.OrderDao">

    <sql id="tOrderColumns">
        a.id AS "id",<!-- 主键 -->
        a.order_no AS "orderNo",<!-- 订单编号 -->
        a.t_customer_id AS "customer.id",<!-- 客户编号 -->
        a.sys_office_id AS "companyOffice.id",<!-- 公司编号 -->
        a.order_date AS "orderDate",<!-- 订单日期 -->
        a.document_status AS "documentStatus",<!-- 订单状态 -->
        a.send_date AS "sendDate",<!-- 发送时间 -->
        a.open_id AS "openId",<!-- 微信编号 -->
        a.create_by AS "createBy.id",<!-- 建立人 -->
        a.create_date AS "createDate",<!-- 建立时间 -->
        a.update_by AS "updateBy.id",<!-- 更新人 -->
        a.update_date AS "updateDate",<!-- 更新时间 -->
        a.remarks AS "remarks",<!-- 备注 -->
        a.del_flag AS "delFlag",<!-- 删除标志 -->
        a.t_sales_entry_id AS "salesEntry.id",<!-- 销售单号 -->
        se.orderno AS "salesEntry.orderno",
        c.name AS "customer.name"<!-- 客户名称 -->
    </sql>

    <sql id="tOrderJoins">
        JOIN t_customer_relation cr ON cr.t_customer_id = a.t_customer_id<!-- 关联客户关系 -->
        JOIN t_customer c ON c.id=a.t_customer_id<!-- 关联客户关系 -->
        LEFT JoIN t_sales_entry se ON se.id=a.t_sales_entry_id<!-- 关联销售单 -->
    </sql>


    <!-- 根据条件取得 订单信息列表 -->
    <select id="findPageOrder" resultType="TOrder">
        SELECT
        <include refid="tOrderColumns" />
        FROM t_order a
        <include refid="tOrderJoins" />
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="userId!=null and userId!=''">
                AND cr.sys_user_id=#{userId}
            </if>
            <if test="id!=null and id!=''">
                AND a.id=#{id}
            </if>
            <if
                test="companyOffice !=null and companyOffice.id!=null and companyOffice.id!=''">
                AND cr.sys_office_id=#{companyOffice.id}
            </if>
            <if test="documentStatus!=null and documentStatus!=''">
                AND a.document_status =#{documentStatus}
            </if>
            <if test="documentStatusList != null"><!-- 根据单据状态查找 -->
                AND a.document_status in
                <foreach item="item" index="index" collection="documentStatusList" open="(" separator="," close=")">
                    #{item}
                </foreach>

            </if>
            <if test="page !=null and page.groupBy != null and page.groupBy != ''">
                group by ${page.groupBy}
            </if>
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''"><!-- 根据 排序字段 排序 -->
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY a.create_date DESC
            </otherwise>
        </choose>

    </select>

原文章地址

猜你喜欢

转载自blog.csdn.net/qq_39584294/article/details/81501655