[英]Union different column in one and then order by this
after a simple query... 经过简单的查询......
SELECT Col_ID, datetime1, datetime2, datetime3
FROM TABLE_DATETIME
WHERE datetime1 LIKE '20130805%'
OR datetime2 LIKE '20130805%'
OR datetime3 LIKE '20130805%'
... I have this scenario ......我有这种情况
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|
Now, in the same previous query, I want to merge the datetime1, datetime2, datetime3 in one columns which we will call "ALL_DATETIME" then order by this to have this results... 现在,在同一个先前的查询中,我想将datetime1,datetime2,datetime3合并到一个列中,我们将其称为“ALL_DATETIME”,然后按此顺序将此结果...
Col_ID |ALL_DATETIME |
----------|--------------|
40302074 |20130805070055|
40302030 |20130805090055|
40302025 |20130805123022|
40302028 |20130805180055|
40302055 |20130805190055|
You can use COALESCE()
function for this: 您可以使用COALESCE()
函数:
SELECT Col_ID
, COALESCE(datetime1,datetime2,datetime3) AS ALL_DATETIME
FROM TABLE_DATETIME
WHERE ...
ORDER BY COALESCE(datetime1,datetime2,datetime3)
Output: 输出:
| COL_ID | ALL_DATETIME |
-----------------------------
| 40302074 | 20130805070055 |
| 40302030 | 20130805090055 |
| 40302025 | 20130805123022 |
| 40302028 | 20130805180055 |
| 40302055 | 20130805190055 |
Use NULL
handling function 使用NULL
处理函数
SELECT Col_ID, ifnull(datetime1, ifnull(datetime2, datetime3 )) as ALL_DATETIME
FROM TABLE_DATETIME
WHERE datetime1 LIKE '20130805%'
OR datetime2 LIKE '20130805%'
OR datetime3 LIKE '20130805%'
order by 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.