[转载]MySQL concat函数的使用

MySQL concat函数是MySQL数据库中众多的函数之一,下文将对MySQL concat函数的语法和使用进行说明,供您参考和学习。

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

返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。

注意:
如果所有参数均为非二进制字符串,则结果为非二进制字符串。 
如果自变量中含有任一二进制字符串,则结果为一个二进制字符串。
一个数字参数被转化为与之相等的二进制字符串格式;若要避免这种情况,可使用显式类型 cast, 例如:
SELECT CONCAT(CAST(int_col AS CHAR), char_col)

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>

posted @ 2015-10-21 15:57  小娜de  阅读(518)  评论(0编辑  收藏  举报