模糊查询(LIKE) CONCAT(字符串连接函数)

来源:互联网 发布:网络机顶盒下载 编辑:程序博客网 时间:2024/05/16 09:48
  1. 如果需要找出u_name中既有“三”又有“猫”的记录,请使用and条件
    SELECT * FROM [user] WHERE u_name LIKE ‘%三%’ AND u_name LIKE ‘%猫%’

若使用 SELECT * FROM [user] WHERE u_name LIKE ‘%三%猫%’
虽然能搜索出“三脚猫”,但不能搜索出符合条件的“张猫三”。

2.SELECT * FROM [user] WHERE u_name LIKE ‘
只找出“唐三藏”这样u_name为三个字且中间一个字是“三”的;

3.SELECT * FROM [user] WHERE u_name LIKE ‘[张李王]三’
将找出“张三”、“李三”、“王三”(而不是“张李王三”);

4.SELECT * FROM [user] WHERE u_name LIKE ‘[^张李王]三’
将找出不姓“张”、“李”、“王”的“赵三”、“孙三”等;

<select id="findList" resultType="Article">        SELECT             <include refid="cmsArticleColumns"/>        FROM cms_article a        <include refid="cmsArticleJoins"/>        <where>            a.del_flag = #{delFlag}            <if test="title != null and title != ''">                AND a.title LIKE                     <if test="dbName == 'oracle'">'%'||#{title}||'%'</if>                    <if test="dbName == 'mssql'">'%'+#{title}+'%'</if>                    **<if test="dbName == 'mysql'">**CONCAT**('%', #{title}, '%')</if>**            </if>            <if test="posid != null and posid != ''">                AND a.posid LIKE                     <if test="dbName == 'oracle'">'%'||#{posid}||'%'</if>                    <if test="dbName == 'mssql'">'%'+#{posid}+'%'</if>                    <if test="dbName == 'mysql'">CONCAT('%', #{posid}, '%')</if>            </if>            <if test="category.id != null and category.id != ''">                AND (a.category_id = #{category.id}                <if test="category.parentIds != null and category.parentIds != ''">                    or c.parent_ids like                         <if test="dbName == 'oracle'">'%'||#{category.id}||'%'</if>                        <if test="dbName == 'mssql'">'%,'+#{category.id}+',%'</if>                        <if test="dbName == 'mysql'">CONCAT('%,', #{category.id}, ',%')</if>                </if>)            </if>            <if test="image != null and image != ''">                AND a.image  = #{image}            </if>            <if test="createBy != null and createBy.id != null and createBy.id != ''">                AND a.create_by  = #{createBy.id}            </if>            <!-- ${sqlMap.dsf}-->        </where>        <choose>            <when test="page !=null and page.orderBy != null and page.orderBy != ''">                ORDER BY ${page.orderBy}            </when>            <otherwise>                ORDER BY a.weight DESC, a.update_date DESC            </otherwise>        </choose>    </select>

SQL— CONCAT(字符串连接函数)

MySQL: CONCAT()
Oracle: CONCAT(), ||
SQL Server: +
CONCAT() 的语法如下:
CONCAT(字串1, 字串2, 字串3, …): 将字串1、字串2、字串3,等字串连在一起。
请注意,Oracle的CONCAT()只允许两个参数;
换言之,一次只能将两个字串串连起来。不过,在Oracle中,我们可以用’||’来一次串连多个字串。
来看几个例子。假设我们有以下的表格:
Geography 表格
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego
例子1:
MySQL/Oracle:

SELECT CONCAT(region_name,store_name) FROM GeographyWHERE store_name = 'Boston';

结果:
‘EastBoston’
例子2:
Oracle:

SELECT region_name || ' ' || store_name FROM GeographyWHERE store_name = 'Boston';

结果:
‘East Boston’
例子3:
SQL Server:

SELECT region_name + ' ' + store_name FROM GeographyWHERE store_name = 'Boston';

结果:
‘East Boston’

0 0