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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM