[轉載]MySQL concat函數的使用

MySQL concat函數是MySQL數據庫中衆多的函數之一,下文將對MySQL concat函數的語法和使用進行說明,供您參考和學習。mysql

MySQL concat函數使用方法:
CONCAT(str1,str2,…) sql

返回結果爲鏈接參數產生的字符串。若有任何一個參數爲NULL ,則返回值爲 NULL。數據庫

注意:
若是全部參數均爲非二進制字符串,則結果爲非二進制字符串。 
若是自變量中含有任一二進制字符串,則結果爲一個二進制字符串。
一個數字參數被轉化爲與之相等的二進制字符串格式;若要避免這種狀況,可以使用顯式類型 cast, 例如:
SELECT CONCAT(CAST(int_col AS CHAR), char_col)mybatis

MySQL concat函數能夠鏈接一個或者多個字符串,如函數

  1. mysql> select concat('10');  
  2. +--------------+  
  3. | concat('10') |  
  4. +--------------+  
  5. | 10   |  
  6. +--------------+  
  7. 1 row in set (0.00 sec)  
  8.  
  9. mysql> select concat('11','22','33');  
  10. +------------------------+  
  11. | concat('11','22','33') |  
  12. +------------------------+  
  13. | 112233 |  
  14. +------------------------+  
  15. 1 row in set (0.00 sec)  

MySQL的concat函數在鏈接字符串的時候,只要其中一個是NULL,那麼將返回NULL學習

    1. mysql> select concat('11','22',null);  
    2. +------------------------+  
    3. | concat('11','22',null) |  
    4. +------------------------+  
    5. | NULL   |  
    6. +------------------------+  
    7. 1 row in set (0.00 sec)  
    8. =====================================================================================
    9. 例子mybatis:
    10. <select id="loadImgAdP" parameterType="map" resultType="map">
    11. select ac.company, date_format(avd.`timeDate`, '%Y-%m-%d') timeDate, ar.des,
      avd.playDay, ad.title, concat(v.cityName, '-', v.countyName, '-', v.streetName, '-', v.villageName) vname
      from ad_village_day avd, advert ad, village v, advert_rule ar, ad_company ac
      WHERE avd.aid = ad.id AND avd.vid = v.id AND ad.rid = ar.id AND ac.id = ad.cid
      <if test="cid != null and cid != ''">
      AND ac.id = #{cid}
      </if>
      <if test="cname != null and cname != ''">
      AND ac.company like concat('%',#{cname},'%')
      </if>
      <if test="villageName != null and villageName != ''">
      AND v.villageName like concat('%',#{vname},'%')
      </if>
      <if test="atitle != null and atitle != ''">
      AND ad.title like concat('%',#{atitle},'%')
      </if>
      <if test="startTime != null and startTime != ''">
      <![CDATA[
      AND date_format(avd.`timeDate`, '%Y%m%d') >= date_format(#{startTime}, '%Y%m%d')
      ]]>
      </if>
      <if test="endTime != null and endTime != ''">
      <![CDATA[
      AND date_format(avd.`timeDate`, '%Y%m%d') <= date_format(#{endTime}, '%Y%m%d')
      ]]>
      </if>
      ORDER BY avd.aid desc ,avd.timeDate, vname
      limit #{pageNo}, #{pageSize}
      </select>
      spa

相關文章
相關標籤/搜索