我有两个具有相同列的表。第一个表让我存储用户的账单,第二个表存储账单取消。
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
如何获得上述结果?
答案 0 :(得分:3)
查询:
<强> SQLFIDDLEExample 强>
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 |
答案 1 :(得分:3)
您可以使用以下代码:
select *, 'OK' AS [Status] from [first table]
UNION ALL
select *, 'Cancel' from [second table]
答案 2 :(得分:3)
使用UNION ALL
集合运算符将两个表合并为一个。
SELECT id, total, 'OK' as status
FROM First_Table
UNION ALL
SELECT id, total, 'CANCEL' as status
FROM Second_Table