简体   繁体   English

MySQL从多行选择值到一行

[英]MySql select values from multiple rows into one row

I have a table with 4 rows with DateTime values saved into. 我有一个带有4行的表,其中保存了DateTime值。

The query: 查询:

SELECT row1, row2, row3, row4 FROM table;

shows this result: 显示此结果:

+------------+------------+------------+------------+
|    row1    |    row2    |    row3    |    row4    |
+------------+------------+------------+------------+
|01.01.2014  |null        |null        |31.12.2018  |
|null        |17.08.2015  |01.12.2050  |null        |
|02.01.2010  |null        |28.03.2067  |null        |
+------------+------------+------------+------------+

But I want to combine and order these dates into one row like: 但我想将这些日期合并并排序为以下一行:

+------------+
|    rows    |
+------------+
|01.01.2014  |
|02.01.2010  |
|17.08.2015  |
|01.12.2050  |
|28.03.2067  |
|31.12.2018  |
+------------+

What query do I have to use to get the result I want? 我必须使用什么查询才能得到想要的结果?

Thanks in advance 提前致谢

SELECT row1 AS rows FROM table WHERE row1 IS NOT NULL
UNION ALL
SELECT row2 AS rows FROM table WHERE row2 IS NOT NULL
UNION ALL
SELECT row3 AS rows FROM table WHERE row3 IS NOT NULL
UNION ALL
SELECT row4 AS rows FROM table WHERE row4 IS NOT NULL
ORDER BY rows DESC;

will do what you want I guess. 我猜你会做你想要的。

Do you have more info about the problem, eg table and column names. 您是否有关于该问题的更多信息,例如表名和列名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM