前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >MYSQL用法(十六) MySQL按指定字段自定义列表排序

MYSQL用法(十六) MySQL按指定字段自定义列表排序

作者头像
Java架构师必看
发布2021-05-31 15:09:14
发布2021-05-31 15:09:14
2.5K00
代码可运行
举报
文章被收录于专栏:Java架构师必看Java架构师必看
运行总次数:0
代码可运行

一、 ORDER BY 排序

要求:按照字段IS_NEW_PROJ的降序、字段LOAN_APR的降序、字段GMT_BUY_START的升序排序

sql语句如下:

代码语言:javascript
代码运行次数:0
运行
复制
SELECT
	b.PROJECT_ID AS project_id,
	b.PROJECT_TITLE AS project_title,
	b.PROJECT_STATUS AS project_status,
	b.LOAN_FUNDS AS loan_funds,
	b.REPAY_ORDER AS repay_order,
	b.LOAN_APR AS loan_apr,
	(
		CASE
		WHEN b.PROJECT_STATUS = 'BUY' THEN 0
		WHEN b.PROJECT_STATUS = 'PREBUY' THEN 1
		WHEN b.PROJECT_STATUS = 'REGISTER' THEN 2
		WHEN b.PROJECT_STATUS = 'FULL_AUDIT' THEN 3
		WHEN b.PROJECT_STATUS = 'BIDS_AUDIT' THEN 4
		WHEN b.PROJECT_STATUS = 'BIDS_CONFIRM' THEN 4
		WHEN b.PROJECT_STATUS = 'DELAY_CONFIRM' THEN 4
		WHEN b.PROJECT_STATUS = 'RUN' THEN 5
		WHEN b.PROJECT_STATUS = 'CANCEL_AUDIT' THEN	6
		WHEN b.PROJECT_STATUS = 'CANCEL_PAYMENT' THEN 6
		WHEN b.PROJECT_STATUS = 'FINISH' THEN 7
		WHEN b.PROJECT_STATUS = 'AUDIT' THEN 8
		WHEN b.PROJECT_STATUS = 'EDIT' THEN 8
		END
	) AS status_order,
	(
		CASE
		WHEN b.PROJECT_STATUS = 'BUY' THEN
			timediff(p.GMT_BUY_END, now())
		WHEN b.PROJECT_STATUS = 'PREBUY' THEN
			timediff(p.GMT_BUY_START, now())
		WHEN b.PROJECT_STATUS = 'REGISTER' THEN
			timediff(p.GMT_REG_START, now())
		END
	) AS time_order,
	p.PUBLISH_TIME AS publishTime,
	p.GMT_BUY_START AS gmt_buy_start,
	p.IS_NEW_PROJ AS isNewProj
FROM
	PJ_BASE_PROJ AS b
LEFT JOIN PJ_MAIN_PROJ AS p ON b.PROJECT_ID = p.PROJECT_ID
WHERE
	b.DELETE_TAG = 'NO'
AND b.PROJECT_STATUS IN (
	'BUY',
	'FULL_AUDIT',
	'RUN',
	'FINISH'
)
ORDER BY
	IS_NEW_PROJ DESC,
	LOAN_APR DESC,
	GMT_BUY_START ASC

结果显示:

二、 ORDER BY FIELD排序(自定义排序规则)

要求:按照字段PROJECT_STATUS指定的顺序排序

sql语句如下:

代码语言:javascript
代码运行次数:0
运行
复制
SELECT
	b.PROJECT_ID AS project_id,
	b.PROJECT_TITLE AS project_title,
	b.PROJECT_STATUS AS project_status,
	b.LOAN_FUNDS AS loan_funds,
	b.REPAY_ORDER AS repay_order,
	b.LOAN_APR AS loan_apr,
	(
		CASE
		WHEN b.PROJECT_STATUS = 'BUY' THEN 0
		WHEN b.PROJECT_STATUS = 'PREBUY' THEN 1
		WHEN b.PROJECT_STATUS = 'REGISTER' THEN 2
		WHEN b.PROJECT_STATUS = 'FULL_AUDIT' THEN 3
		WHEN b.PROJECT_STATUS = 'BIDS_AUDIT' THEN 4
		WHEN b.PROJECT_STATUS = 'BIDS_CONFIRM' THEN 4
		WHEN b.PROJECT_STATUS = 'DELAY_CONFIRM' THEN 4
		WHEN b.PROJECT_STATUS = 'RUN' THEN 5
		WHEN b.PROJECT_STATUS = 'CANCEL_AUDIT' THEN	6
		WHEN b.PROJECT_STATUS = 'CANCEL_PAYMENT' THEN 6
		WHEN b.PROJECT_STATUS = 'FINISH' THEN 7
		WHEN b.PROJECT_STATUS = 'AUDIT' THEN 8
		WHEN b.PROJECT_STATUS = 'EDIT' THEN 8
		END
	) AS status_order,
	(
		CASE
		WHEN b.PROJECT_STATUS = 'BUY' THEN
			timediff(p.GMT_BUY_END, now())
		WHEN b.PROJECT_STATUS = 'PREBUY' THEN
			timediff(p.GMT_BUY_START, now())
		WHEN b.PROJECT_STATUS = 'REGISTER' THEN
			timediff(p.GMT_REG_START, now())
		END
	) AS time_order,
	p.PUBLISH_TIME AS publishTime,
	p.GMT_BUY_START AS gmt_buy_start,
	p.IS_NEW_PROJ AS isNewProj
FROM
	PJ_BASE_PROJ AS b
LEFT JOIN PJ_MAIN_PROJ AS p ON b.PROJECT_ID = p.PROJECT_ID
WHERE
	b.DELETE_TAG = 'NO'
AND b.PROJECT_STATUS IN (
	'BUY',
	'FULL_AUDIT',
	'RUN',
	'FINISH'
)
ORDER BY FIELD(PROJECT_STATUS,'BUY','FULL_AUDIT','RUN','FINISH')

结果显示:

三、 ORDER BY FIELD排序升级(多条件组合排序)

要求:按照字段IS_NEW_PROJ降序、字段PROJECT_STATUS指定的顺序排、字段LOAN_APR降序、字段GMT_BUY_START升序

sql语句如下:

代码语言:javascript
代码运行次数:0
运行
复制
SELECT
	b.PROJECT_ID AS project_id,
	b.PROJECT_TITLE AS project_title,
	b.PROJECT_STATUS AS project_status,
	b.LOAN_FUNDS AS loan_funds,
	b.REPAY_ORDER AS repay_order,
	b.LOAN_APR AS loan_apr,
	(
		CASE
		WHEN b.PROJECT_STATUS = 'BUY' THEN 0
		WHEN b.PROJECT_STATUS = 'PREBUY' THEN 1
		WHEN b.PROJECT_STATUS = 'REGISTER' THEN 2
		WHEN b.PROJECT_STATUS = 'FULL_AUDIT' THEN 3
		WHEN b.PROJECT_STATUS = 'BIDS_AUDIT' THEN 4
		WHEN b.PROJECT_STATUS = 'BIDS_CONFIRM' THEN 4
		WHEN b.PROJECT_STATUS = 'DELAY_CONFIRM' THEN 4
		WHEN b.PROJECT_STATUS = 'RUN' THEN 5
		WHEN b.PROJECT_STATUS = 'CANCEL_AUDIT' THEN	6
		WHEN b.PROJECT_STATUS = 'CANCEL_PAYMENT' THEN 6
		WHEN b.PROJECT_STATUS = 'FINISH' THEN 7
		WHEN b.PROJECT_STATUS = 'AUDIT' THEN 8
		WHEN b.PROJECT_STATUS = 'EDIT' THEN 8
		END
	) AS status_order,
	(
		CASE
		WHEN b.PROJECT_STATUS = 'BUY' THEN
			timediff(p.GMT_BUY_END, now())
		WHEN b.PROJECT_STATUS = 'PREBUY' THEN
			timediff(p.GMT_BUY_START, now())
		WHEN b.PROJECT_STATUS = 'REGISTER' THEN
			timediff(p.GMT_REG_START, now())
		END
	) AS time_order,
	p.PUBLISH_TIME AS publishTime,
	p.GMT_BUY_START AS gmt_buy_start,
	p.IS_NEW_PROJ AS isNewProj
FROM
	PJ_BASE_PROJ AS b
LEFT JOIN PJ_MAIN_PROJ AS p ON b.PROJECT_ID = p.PROJECT_ID
WHERE
	b.DELETE_TAG = 'NO'
AND b.PROJECT_STATUS IN (
	'BUY',
	'FULL_AUDIT',
	'RUN',
	'FINISH'
)
ORDER BY FIELD(IS_NEW_PROJ,'DESC',PROJECT_STATUS,'BUY','FULL_AUDIT','RUN','FINISH',LOAN_APR,'DESC',GMT_BUY_START,'ASC')

结果显示: -->此时结果集是混乱的

四、 ORDER BY 排序终极版(多条件组合排序+自定义排序)

要求:按照字段IS_NEW_PROJ降序、字段PROJECT_STATUS指定的顺序排序、字段GMT_BUY_START降序、字段PROJECT_STATUS指定的顺序排序

sql语句如下:

代码语言:javascript
代码运行次数:0
运行
复制
SELECT
	b.PROJECT_ID AS project_id,
	b.PROJECT_TITLE AS project_title,
	b.PROJECT_STATUS AS project_status,
	b.LOAN_FUNDS AS loan_funds,
	b.LOAN_APR AS loan_apr,
	(
		CASE
		WHEN b.PROJECT_STATUS = 'BUY' THEN 0
		WHEN b.PROJECT_STATUS = 'PREBUY' THEN 1
		WHEN b.PROJECT_STATUS = 'REGISTER' THEN 2
		WHEN b.PROJECT_STATUS = 'FULL_AUDIT' THEN 3
		WHEN b.PROJECT_STATUS = 'BIDS_AUDIT' THEN 4
		WHEN b.PROJECT_STATUS = 'BIDS_CONFIRM' THEN 4
		WHEN b.PROJECT_STATUS = 'DELAY_CONFIRM' THEN 4
		WHEN b.PROJECT_STATUS = 'RUN' THEN 5
		WHEN b.PROJECT_STATUS = 'CANCEL_AUDIT' THEN	6
		WHEN b.PROJECT_STATUS = 'CANCEL_PAYMENT' THEN 6
		WHEN b.PROJECT_STATUS = 'FINISH' THEN 7
		WHEN b.PROJECT_STATUS = 'AUDIT' THEN 8
		WHEN b.PROJECT_STATUS = 'EDIT' THEN 8
		END
	) AS status_order,
	(
		CASE
		WHEN b.PROJECT_STATUS = 'BUY' THEN
			timediff(p.GMT_BUY_END, now())
		WHEN b.PROJECT_STATUS = 'PREBUY' THEN
			timediff(p.GMT_BUY_START, now())
		WHEN b.PROJECT_STATUS = 'REGISTER' THEN
			timediff(p.GMT_REG_START, now())
		END
	) AS time_order,
	p.PUBLISH_TIME AS publishTime,
	p.GMT_BUY_START AS gmt_buy_start,
	p.IS_NEW_PROJ AS isNewProj
FROM
	PJ_BASE_PROJ AS b
LEFT JOIN PJ_MAIN_PROJ AS p ON b.PROJECT_ID = p.PROJECT_ID
WHERE
	b.DELETE_TAG = 'NO'
AND b.PROJECT_STATUS IN (
	'BUY',
	'FULL_AUDIT',
	'RUN',
	'FINISH'
)
ORDER BY
	IS_NEW_PROJ DESC,
	STATUS_ORDER ASC,
	p.GMT_BUY_START DESC,
	TIME_ORDER ASC

结果显示:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档