获取同一1列表中不同列的值

时间:2022-06-28 13:21:22

I am working on Ms Sql Server 2008 R2. now i want one column which has results of 3 different columns of same table.. Let me explain with Figure.

我正在研究Sql Server 2008 R2。现在我想要一个列具有相同表的3个不同列的结果。让我用图解释。

Table: mainTable

Id    Column1     Column2     Column3
-------------------------------------
 1    urla        urlb         urlc
 2    urld        urle         urlf
 3    urlg        urlh         urli

Now i want one column

现在我想要一个专栏

Table Name: ResultTable

表名:ResultTable

Id    ColumnResult  
-------------
 1    urla 
 2    urlb
 3    urlc
 4    urld
 5    urle
 6    urlf
 7    urlg
 8    urlh
 9    urli

Thanks in Advance. Sahil Patel

提前致谢。萨希尔帕特尔

1 个解决方案

#1


1  

You can use UNION ALL:

你可以使用UNION ALL:

SELECT
    Id = ROW_NUMBER() OVER(ORDER BY ColumnResult),
    Column1
FROM (
    SELECT Id, Column1 AS ColumnResult FROM mainTable UNION ALL
    SELECT Id, Column2 FROM mainTable UNION ALL
    SELECT Id, Column3 FROM mainTable
)t

#1


1  

You can use UNION ALL:

你可以使用UNION ALL:

SELECT
    Id = ROW_NUMBER() OVER(ORDER BY ColumnResult),
    Column1
FROM (
    SELECT Id, Column1 AS ColumnResult FROM mainTable UNION ALL
    SELECT Id, Column2 FROM mainTable UNION ALL
    SELECT Id, Column3 FROM mainTable
)t