mysql 截取數據 組合排序


  1. mysql 按照某個字段是空 組合排序
    如下可實現:按照 AWARD_AMOUNT 先是空的, BET_AMOUNT 再倒序

     SELECT * FROM `single_bet` order by  AWARD_AMOUNT is null desc,BET_AMOUNT desc;
    
  2. mybatis傳遞數組循環構建in語句

     <foreach collection="stateTypeArr" index="index" item="item" open="b.STATE_TYPE IN(" separator="," close=")">
         #{item}
     </foreach>
    
  3. mybatis where標簽 【原文鏈接

     <select id="findActiveBlogLike" resultType="Blog">
       SELECT * FROM BLOG 
       <where> 
         <if test="state != null">
              state = #{state}
         </if> 
         <if test="title != null">
             AND title like #{title}
         </if>
         <if test="author != null and author.name != null">
             AND author_name like #{author.name}
         </if>
       </where>
     </select>
    

    where 元素知道只有在一個以上的if條件有值的情況下才去插入“WHERE”子句。而且,若最后的內容是“AND”或“OR”開頭的,where 元素也知道如何將他們去除。

    如果 where 元素沒有按正常套路出牌,我們還是可以通過自定義 trim 元素來定制我們想要的功能。比如,和 where 元素等價的自定義 trim 元素為:

     <trim prefix="WHERE" prefixOverrides="AND |OR "> ... </trim>
    
  4. mysql截取字符串轉成數字並比較排序 【原文鏈接

     SUBSTRING_INDEX([列名],[分割符],[段數])
     列名:要分割列里內容的列名
     分割符:用來切割的符號
     段數:切割后取的長度
      
     以下示例說明參數:
     表info
     列c_code
     值
     1-10-ache
     則
     select SUBSTRING_INDEX(c_code,'-',1) as c_code from info
     會輸出
     c_code
     1
     而select SUBSTRING_INDEX(c_code,'-',2) as c_code from info
     會輸出
     1-10
     select SUBSTRING_INDEX(c_code,'-',-1) as c_code from info
     會輸出
     ache
     這里-1跟高級語言中字符串截取一樣,同樣負數表示從后面開始計算
      
     排序,則
     1-10-ache
     1-2-ache
     2-11-ache
     2-3-ache
     2-5-ache
      
     select * from info order by (SUBSTRING_INDEX(c_code,'-',1)+0),(SUBSTRING_INDEX(SUBSTRING_INDEX(c_code,'-',2),'-',-1)+0) asc
      
     輸出
     1-2-ache
     1-10-ache
     2-3-ache
     2-5-ache
     2-11-ache
      
     利用雙重截取,之后利用mysql特性(+0會自動轉化也數字),作數值的大小比較


免責聲明!

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



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