【问题标题】:Mysql how to select values from diffent columns into an single column amoung each otherMysql如何将不同列中的值相互选择到单个列中
【发布时间】:2017-02-08 21:04:18
【问题描述】:

我有一张这样的桌子:

+------+-------+
|yearIn|yearOut|
+------+-------+
|1974  |2012   |
+------+-------+
|1935  |2020   |
+------+-------+
|1980  |1999   |
+------+-------+

我需要选择所有这些年份并将它们放在这样的一列中:

+------+
|years |
+------+
|1974  |
+------+
|1935  |
+------+
|1980  |
+------+
|2012  |
+------+
|2020  |
+------+
|1999  |
+------+

在此先感谢您的帮助 :-)

【问题讨论】:

    标签: mysql select multiple-columns


    【解决方案1】:

    你可以用这样的查询来做到这一点:

    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>
    

    【讨论】:

    • 为什么是子查询?另外,谁说我们想要重复?
    • @shmosel - 重复 ??
    • 示例中没有,但有可能。
    • OP 说 我需要选择所有这些年,所以他会看到所有
    • 我不同意这种解释,但争论它没有意义。您还没有解释对子查询的需求。
    猜你喜欢
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 2021-03-30
    • 2016-05-14
    • 2012-05-25
    相关资源
    最近更新 更多