我在sql-server-2008中有两个sql视图。两者都有ITEM_NO
,QTY
和LOCATION
列。
view_1:
-----------------------------------
| ITEM_NO | QTY | LOCATION |
-----------------------------------
| SN-15 | 0 | SEA |
-----------------------------------
| XY-45 | 2 | SFO |
-----------------------------------
| TY-14 | 1 | SEA |
-----------------------------------
view_2:
-----------------------------------
| ITEM_NO | QTY | LOCATION |
-----------------------------------
| HN-34 | 1 | SEA |
-----------------------------------
| GJ-13 | 4 | SFO |
-----------------------------------
| RB-02 | 7 | SEA |
-----------------------------------
| AJ-13 | 9 | LAX |
-----------------------------------
我想将这些组合成一个视图,如下所示:
-----------------------------------
| ITEM_NO | QTY | LOCATION |
-----------------------------------
| SN-15 | 0 | SEA |
-----------------------------------
| XY-45 | 2 | SFO |
-----------------------------------
| TY-14 | 1 | SEA |
-----------------------------------
| HN-34 | 1 | SEA |
-----------------------------------
| GJ-13 | 4 | SFO |
-----------------------------------
| RB-02 | 7 | SEA |
-----------------------------------
| AJ-13 | 9 | LAX |
-----------------------------------
有一种简单的方法吗?
答案 0 :(得分:1)
试试这个
SELECT ITEM_NO, QTY, LOCATION
FROM view_1
UNION ALL
SELECT ITEM_NO, QTY, LOCATION
FROM view_2
答案 1 :(得分:1)
您可以使用UNION
..
SELECT ITEM_NO, QTY, LOCATION from View_1
UNION
SELECT ITEM_NO, QTY, LOCATION from View_2
或,如果您不关心重复,更有效的方法是UNION ALL
SELECT ITEM_NO, QTY, LOCATION from View_1
UNION ALL
SELECT ITEM_NO, QTY, LOCATION from View_2
UNION
将在内部执行DISTINCT
,而UNION ALL
则不会,因此效果会更好。您还应注意,如果您希望ORDER BY
对整个结果集进行排序,则ORDER BY
需要放置在查询的 END 处。
答案 2 :(得分:1)