左连接右连接 子查询!! 动态sql

更新
我的妈呀
简直是个大笨蛋,我的妈呀,肿么简单的sql都不会,我滴乖还想半天呢还觉得难的再次哭唧唧55555欠下的债总要来还的mmmm居然靠丢人来还哇哦
肿么简单的一个子查询,哎叹气。。。。。对不起我浩哥
select * from t_apply where courseorchapter_id in (select chapter_id from t_chapter where course_id=1) and apply_type=1

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语句,具体是哪个连接要按

具体的业务而定。




<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>

原文章地址

你可能感兴趣的:(2018.08总结)