Mysql 如何 select 将来自不同列的值彼此合并到一个列中
Mysql how to select values from diffent columns into an single column amoung each other
我有一个 table 这样的:
+------+-------+
|yearIn|yearOut|
+------+-------+
|1974 |2012 |
+------+-------+
|1935 |2020 |
+------+-------+
|1980 |1999 |
+------+-------+
这些年来我需要 select 并且像这样将它们放在一栏中:
+------+
|years |
+------+
|1974 |
+------+
|1935 |
+------+
|1980 |
+------+
|2012 |
+------+
|2020 |
+------+
|1999 |
+------+
在此先感谢您的帮助:-)
您可以使用这样的查询来完成:
SELECT Years
FROM (
SELECT yearIn AS Years FROM yourTable
UNION ALL
SELECT yearOut FROM yourTable
) y
ORDER BY Years;
你也可以在没有子查询的情况下使用它
SELECT yearIn AS Years FROM yourTable
UNION ALL
SELECT yearOut FROM yourTable
ORDER BY Years;
带和不带子查询的 EXPLAIN
mysql> EXPLAIN SELECT Years
-> FROM (
-> SELECT yearIn AS Years FROM yourTable
-> UNION ALL
-> SELECT yearOut FROM yourTable
-> ) y
-> ORDER BY Years;
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+----------------+
| 1 | PRIMARY | <derived2> | NULL | ALL | NULL | NULL | NULL | NULL | 6 | 100.00 | Using filesort |
| 2 | DERIVED | yourTable | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL |
| 3 | UNION | yourTable | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL |
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+----------------+
3 rows in set, 1 warning (0,00 sec)
mysql>
mysql> EXPLAIN SELECT yearIn AS Years FROM yourTable
-> UNION ALL
-> SELECT yearOut FROM yourTable
-> ORDER BY Years;
+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+---------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+---------------------------------+
| 1 | PRIMARY | yourTable | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL |
| 2 | UNION | yourTable | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL |
| NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary; Using filesort |
+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+---------------------------------+
3 rows in set, 1 warning (0,00 sec)
mysql>
我有一个 table 这样的:
+------+-------+
|yearIn|yearOut|
+------+-------+
|1974 |2012 |
+------+-------+
|1935 |2020 |
+------+-------+
|1980 |1999 |
+------+-------+
这些年来我需要 select 并且像这样将它们放在一栏中:
+------+
|years |
+------+
|1974 |
+------+
|1935 |
+------+
|1980 |
+------+
|2012 |
+------+
|2020 |
+------+
|1999 |
+------+
在此先感谢您的帮助:-)
您可以使用这样的查询来完成:
SELECT Years
FROM (
SELECT yearIn AS Years FROM yourTable
UNION ALL
SELECT yearOut FROM yourTable
) y
ORDER BY Years;
你也可以在没有子查询的情况下使用它
SELECT yearIn AS Years FROM yourTable
UNION ALL
SELECT yearOut FROM yourTable
ORDER BY Years;
带和不带子查询的 EXPLAIN
mysql> EXPLAIN SELECT Years
-> FROM (
-> SELECT yearIn AS Years FROM yourTable
-> UNION ALL
-> SELECT yearOut FROM yourTable
-> ) y
-> ORDER BY Years;
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+----------------+
| 1 | PRIMARY | <derived2> | NULL | ALL | NULL | NULL | NULL | NULL | 6 | 100.00 | Using filesort |
| 2 | DERIVED | yourTable | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL |
| 3 | UNION | yourTable | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL |
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+----------------+
3 rows in set, 1 warning (0,00 sec)
mysql>
mysql> EXPLAIN SELECT yearIn AS Years FROM yourTable
-> UNION ALL
-> SELECT yearOut FROM yourTable
-> ORDER BY Years;
+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+---------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+---------------------------------+
| 1 | PRIMARY | yourTable | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL |
| 2 | UNION | yourTable | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL |
| NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary; Using filesort |
+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+---------------------------------+
3 rows in set, 1 warning (0,00 sec)
mysql>