1

我有 2 个具有相同列的表。第一个表让我存储用户的账单,第二个表存储账单取消。

first table
-----------
id  - total
-----------
1   -   100
2   -    85
3   -    50

second table
-----------
id  - total
-----------
2   -    85


Result of JOIN
------------------------ 
id  - total    -  status
------------------------
1   -   100    -   OK
2   -    85    -   OK
3   -    50    -   OK
2   -    85    -   CANCEL

我怎样才能得到上述结果?

4

3 回答 3

3

询问:

SQLFIDDLE示例

SELECT t1.id,
       t1.total,
       'OK' AS status
FROM first t1
UNION ALL
SELECT t2.id,
       t2.total,
       'CANCEL' AS status
FROM second t2

结果:

| ID | TOTAL | STATUS |
-----|-------|--------|
|  1 |   100 |     OK |
|  2 |    85 |     OK |
|  3 |    50 |     OK |
|  2 |    85 | CANCEL |
于 2013-08-29T13:10:48.087 回答
3

您可以使用以下代码:

select *, 'OK' AS [Status] from [first table]
UNION ALL
select *, 'Cancel' from [second table]
于 2013-08-29T13:11:06.993 回答
3

使用UNION ALL集合运算符将两个表合并为一个。

SELECT id, total, 'OK' as status
FROM First_Table
UNION ALL
SELECT id, total, 'CANCEL' as status
FROM Second_Table
于 2013-08-29T13:11:09.493 回答