我正在尝试在我的存储过程中加入这两个语句。我该怎么做才能获得一个表,以便我可以使用groupBy列。
Select
TableA.Name,
TableB.Occupation,
'Group1' AS [groupBy]
From
TableA,
TableB
Where
TableA.ID = 1
Select
TableA.Name,
TableB.Occupation,
'Group2' AS [groupBy]
From
TableA,
TableB
Where
TableB.ID = 10
我的结果应该是这样的
Name Occupation GroupBy
David Doctor Group1
John Pilot Group1
Dwayne Wrestler Group2
Axel RockStar Group2
更新
Table A
ID Name Occupation
1 David Doctor
1 John Pilot
2 Mike Clerk
Table B
ID Name Occupation
3 Wayne Writer
4 Shane Publisher
10个Dwayne Wrestler
10 Axel Rockstar
答案 0 :(得分:2)
您似乎可以使用UNION ALL
来获得结果:
Select
TableA.Name,
TableA.Occupation,
'Group1' AS [groupBy]
From TableA
Where TableA.ID = 1
union all
Select
TableB.Name,
TableB.Occupation,
'Group2' AS [groupBy]
From TableB
Where TableB.ID = 10
见SQL Fiddle with Demo。 UNION ALL
将返回两个查询之间的所有行。如果您认为可能有要删除的重复项,则可以使用UNION
。