How to merge two columns in sql and display it into a separate record

后端 未结 3 509
臣服心动
臣服心动 2021-01-26 08:49

I have this table

Column1|Column2
1       2
3       4

I want to merge two columns and make them appear like this

 NewColumn
 1         


        
相关标签:
3条回答
  • 2021-01-26 09:08

    The keyword you're looking for is UNION:

    SELECT column 1 AS newColumn
    FROM...
    UNION
    SELECT column2 AS newColumn
    FROM...
    ORDER BY newColumn
    

    This of course gets unwieldy if you need to combine multiple columns - ideally avoid having to do it by storing data as raw as possible and aggregating instead.

    0 讨论(0)
  • 2021-01-26 09:11

    Maybe you could use UNION :

    SELECT Column1 FROM table UNION SELECT Column2 FROM table
    

    Not sure to understand if that's what you want...

    0 讨论(0)
  • 2021-01-26 09:15

    I would advice to do this using php or some script but not using mysql. Still, you can achieve this with mysql like this:

    SELECT t1.Column1 FROM table as t1
    UNION
    SELECT t2.Column2 as column1 FROM table as t2 ORDER BY Column1;
    
    0 讨论(0)
提交回复
热议问题