jeecg项目sql语句积累

1.多表联表查询

select a.device_code,a.device_name,a.done_person,a.task_code,a.child_code,b.compoInvCode from

  (SELECT
  a.device_code,a.device_name,a.done_person,a.task_code,b.child_code
  FROM
  gx_device a
  JOIN gx_device_detail b ON a.device_code =
  b.device_code) a

join xf_component_invcode b on a.child_code = b.compoNumber

核心思想:两个表的联立,用join加上连接条件就行。如果三个表连接,就先把两个表连接起来,把连接起来的结果用括号括起来当一个整体,再连接第三个表,就回到了两个表连接的情况,这种把结果当一个整体的表的方法可以解决任意多个表的连接问题。也可一次性join连接多表,转9条。

2.distinct 加在select之后,可去重复

3.limit + 数字   放在最后,可只读取数字相应的行 比如 limit 10 读取前10行

4.and 的优先级比or的优先级高,在给条件时要注意

5.left join 取到等值条件后会加入左边表的不满足等值条件的部分,right join 取到等值条件后会加入右边表的不满足等值条件的部分,inner join 只取到满足等值条件的部分。

6.动态sql语句,常见情况:多条件查询,给定越多的条件,查询越精确。 

如:

<select id="passDeviceInfoToQuery" parameterType="java.lang.String"
        resultType="org.jeecg.modules.stasticsandquery.entity.DeviceInfo"

        SELECT distinct
        a.device_code,b.material_code,b.material_name,a.own_order,b.order_status
        as
        'taskStatus' FROM
        gx_device_main a
        JOIN xf_task_order b
        ON
        a.material_code =
        b.material_code
        <where>
            <if test="deviceCode != null and deviceCode != ''">
                a.device_code = #{deviceCode}
            </if>
            <if test="material != null and material != ''">
                AND (b.material_code = #{material} or b.material_name =
                #{material})
            </if>
            <if test="taskStatus != null and taskStatus != ''">
                and b.order_status = #{taskStatus}
            </if>
            <if test="ownOrder != null and ownOrder != ''">
                and a.own_order = #{ownOrder}
            </if>
        </where>
    </select>

<if test="deviceCode != null and deviceCode != ''">

 a.device_code = #{deviceCode}

</if>

<if test=""> if标签做条件判断

<where> 标签可去掉多出来的and关键字

7.order by + 字段名 排序 默认按asc升序排序,指定desc时按降序排序。

8.https://www.jianshu.com/p/8b135d373df1             group by的使用。

9.https://blog.csdn.net/wyqwilliam/article/details/83545172     多表联合查询

 

10.https://www.w3school.com.cn/sql/sql_union.asp      union的使用

11. mybaties动态sql choose标签 和when标签 可以实现按条件执行某部分语句

select
        task_code,material_code,material_name,deliver_qty,finish_qty
        from
        <choose>
            <when test="'1'.toString()==taskcategy.toString()">xf_task_order</when>
            <when test="'2'.toString()==taskcategy.toString()">gx_task_transformdetail</when>
        </choose>
        where task_code = #{taskcode}

12.https://blog.csdn.net/weixin_39633383/article/details/78674135 sql语句字符串拼接

13.https://blog.csdn.net/yueqi1125/article/details/79478143 union又去重复的功能 不去重复时使用union all

14.https://www.cnblogs.com/dancesir/p/8888071.html distinct 具备指定的字段一起去重复的功能,可单列去重复,可多列去重复。

15,https://www.cnblogs.com/qingmuchuanqi48/p/11723520.htm   

sql语句查询多条件与效果  

order by后边的字段并不是唯一的,支持多个,按照你排序的先后顺序写就可以了。另外按照每个字段的升序和降序同样支持。默认是升序的。如下
order by column1(asc or desc),column2(asc or desc),column3(asc or desc),column4(asc or desc)...

原文地址:https://www.cnblogs.com/jianghuxiao/p/11542140.html