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

  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 Geography
WHERE store_name = 'Boston';

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

SELECT region_name || ' ' || store_name FROM Geography
WHERE store_name = 'Boston';

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

SELECT region_name + ' ' + store_name FROM Geography
WHERE store_name = 'Boston';

结果:
‘East Boston’

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值