按2个日期字段排序

时间:2015-12-22 13:14:18

标签: mysql datetime

表:

id | date_in             | date_out            |
1  | 2016-01-01 12:00:00 | 2016-01-03 12:00:00 |
2  | 2016-01-01 12:00:00 | 2016-01-02 12:00:00 |

我需要一个返回结果的查询:

id | alias
1  | 2016-01-01 12:00:00
2  | 2016-01-01 12:00:00
2  | 2016-01-02 12:00:00
1  | 2016-01-03 12:00:00

3 个答案:

答案 0 :(得分:5)

我会假设union的某些内容就足够了,因为没有上下文我只能假设这是你正在寻找的东西?

SELECT t.id, t.date_in AS alias FROM test t
UNION ALL
SELECT t2.id, t2.date_out AS alias FROM test t2
ORDER BY alias ASC;

会给你一个如下结果:

+----+---------------------+
| id | alias               |
+----+---------------------+
|  2 | 2016-01-01 12:00:00 |
|  1 | 2016-01-01 12:00:00 |
|  2 | 2016-01-02 12:00:00 |
|  1 | 2016-01-03 12:00:00 |
+----+---------------------+
4 rows in set (0.00 sec)

答案 1 :(得分:0)

请尝试以下

SELECT

CONCAT(date_in,date_out)为Alias    FROM table_name;

答案 2 :(得分:0)

使用像Ash建议的UNION ALL查询回答了这个问题。