在SQL中合并不同查询的结果集

时间:2011-08-10 11:33:28

标签: sql sql-server tsql

有没有办法合并来自不同查询的结果集?例如

Query A

SELECT DISTINCT HighEnd FROM Chipset

    HighEnd
    ------------
    Class A
    Class B
    Class C

Query B

SELECT DISTINCT LowEnd FROM VideoCard

    LowEnd
    ------------
    Class X
    Class Y
    Class Z

并使其像这样

    CombinedSets
    ------------
    Class A
    Class B
    Class C
    Class X
    Class Y
    Class Z

2 个答案:

答案 0 :(得分:2)

SELECT DISTINCT HighEnd AS CombinedSets FROM Chipset
UNION
SELECT DISTINCT LowEnd AS CombinedSets FROM VideoCard

您可以使用UNION来合并结果。这仅显示两者的不同值。如果您需要重复项,则需要使用UNION ALL

答案 1 :(得分:1)

Union或Union All

SELECT DISTINCT HighEnd as CombinedSets FROM Chipset
UNION
SELECT DISTINCT LowEnd FROM VideoCard