mysql自定义排序

本文通过一段MyBatis SQL代码示例介绍了如何实现自定义排序功能,特别是使用FIELD函数来进行特定顺序的排序。

先来一段变态代码:

<select id="selectSerial" parameterType="PageData" resultType="PageData">
    	select 
			cs_id,	
			bs_id,	
			cb_id,	
			cs_show_name,	
			cb_Name,	
			mb_Name,	
			logo,	
			cs_pic,	
			country,	
			type,	
			displacement,	
			level,	
			max_price,	
			min_price,	
			create_by,	
			update_by,	
			create_time,	
			update_time,	
			is_deleted,	
			dealer_update_time,	
			CsPriceRange
    FROM car_serial WHERE 1=1
        
     	<if test="cb_Name != '' and cb_Name != null">  
	     	<if test="cb_Name == '丰田'">  
	            and cb_id in(20057, 10020)
	        </if>
	        <if test="cb_Name != '丰田'">
	            and cb_Name LIKE '%${cb_Name}%' 
	        </if>
        </if>  
        <if test="level != '' and level != null">  
            and `level` like #{level}
        </if>
        
        AND cs_pic != 'http://image.bitautoimg.com/autoalbum/V2.1/images/150-100.gif'
        
       	<if test="cb_Name == '奥迪'">  
             order by cb_id
        </if>
        
        <if test="cb_Name == '宝马'">  
        	order by FIELD(`cb_id`, '20066', '20218', '20005')
       	</if>
  </select>

自定义排序可以根据字段里的数据进行排序,实在是太好用了

语法如下:

order by FIELD(`cb_id`, '20066', '20218', '20005')

转载于:https://my.oschina.net/hehongbo/blog/876121

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值