数据库
首页 > 数据库> > MySQL order by 条件排序 if/case...when

MySQL order by 条件排序 if/case...when

作者:互联网

1、常规语法查询结果展示:
2、使用 order by 配合if语句
SELECT
	* 
FROM
	prod_bread 
ORDER BY
IF
	( arrange_sn > 0, 0, 1 ),
	arrange_sn ASC,
	id DESC;

其效果等同于 case…when 语句

SELECT
	* 
FROM
	`prod_bread` 
ORDER BY
CASE
		`prod_bread`.`arrange_sn` 
		WHEN `prod_bread`.`arrange_sn` > 0 THEN
		0 ELSE 1 
	END,
	`prod_bread`.`arrange_sn`,
	`prod_bread`.`id` DESC;

标签:case,...,降序,sn,MySQL,bread,prod,id,arrange
来源: https://blog.csdn.net/wub116/article/details/86528722