gpt4 book ai didi

mysql - 将不同的列合并为一列,然后按此排序

转载 作者:行者123 更新时间:2023-11-29 08:25:33 24 4
gpt4 key购买 nike

简单查询后...

SELECT Col_ID, datetime1, datetime2, datetime3 
FROM TABLE_DATETIME
WHERE datetime1 LIKE '20130805%'
OR datetime2 LIKE '20130805%'
OR datetime3 LIKE '20130805%'

...我有这个场景

Col_ID    |datetime1     |datetime2     |datetime3     |
----------|--------------|--------------|--------------|
40302025 |20130805123022|NULL |NULL |
40302028 |20130805180055|NULL |NULL |
40302030 |NULL |20130805090055|NULL |
40302055 |NULL |20130805190055|NULL |
40302074 |NULL |NULL |20130805070055|

现在,在之前的同一个查询中,我想将 datetime1、datetime2、datetime3 合并到一列中,我们将其称为“ALL_DATETIME”,然后按此排序以获得此结果...

Col_ID    |ALL_DATETIME  |
----------|--------------|
40302074 |20130805070055|
40302030 |20130805090055|
40302025 |20130805123022|
40302028 |20130805180055|
40302055 |20130805190055|

最佳答案

您可以使用COALESCE()功能:

SELECT Col_ID
, COALESCE(datetime1,datetime2,datetime3) AS ALL_DATETIME
FROM TABLE_DATETIME
WHERE ...
ORDER BY COALESCE(datetime1,datetime2,datetime3)

输出:

|   COL_ID |   ALL_DATETIME |
-----------------------------
| 40302074 | 20130805070055 |
| 40302030 | 20130805090055 |
| 40302025 | 20130805123022 |
| 40302028 | 20130805180055 |
| 40302055 | 20130805190055 |

参见this SQLFiddle

关于mysql - 将不同的列合并为一列,然后按此排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18056403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com