MySQL中文排序

在我们的项目开发中经常会遇到按照名称A-Z等SQL中文排序查找问题,在这里记录一下:

<if test="null != orderField and orderField != '' and  null != orderType and orderType != ''">
        ORDER BY 
        CONVERT( ${orderField} USING GBK)
        ${orderType}
</if>

有时候我们会做连表查询或表字段的聚合函数计算等就要具体的表字段排序,而不是${orderField}引用 否则会报错:

SELECT
        w.f_id AS id, w.f_title AS title, u.f_name AS createdName, u.f_account AS account
        FROM  tablew w
        LEFT JOIN tuser u ON u.f_id = w.f_created_by
        <where>
            <if test="null != ...">
                ...
            </if>
        </where>
        <if test="null != orderField and orderField != '' and  null != orderType and orderType != ''">
            ORDER BY
            <choose>
                <when test="orderField == 'f_title' or orderField == 'createdName'">
                    CONVERT(${orderField} USING GBK)
                </when> 
                <otherwise>
                    w.${orderField}
                </otherwise>
            </choose>
            ${orderType}
        </if>

如上代码则会报错,CONVERT(${orderField} USING GBK) 需要排序明确表字段w.f_title

            <choose>
                <when test="orderField == 'f_title'">
                    CONVERT(w.f_title USING GBK)
                </when>
                <otherwise>
                    w.${orderField}
                </otherwise>
            </choose>
            ${orderType}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值