我有一个包含三列的表:
SELECT * FROM table1 WHERE status="ok"
# Result 1
/*--------------------------------------------
qnt name status
10 prod1 ok
10 prod2 ok
--------------------------------------------*/
SELECT * FROM table1 WHERE status="deleted"
# Result 2
/*--------------------------------------------
qnt name status
2 prod1 deleted
3 prod2 deleted
--------------------------------------------*/
我想要这样的结果(有可能吗?):
# Result3 = (Result 1) - (Result 2)
/*--------------------------------------------
qnt name
8 prod1
7 prod2
--------------------------------------------*/
答案 0 :(得分:1)
SELECT
ok.qnt - deleted.qnt AS qnt,
ok.name
FROM
(SELECT * FROM table1 WHERE status="ok") ok
LEFT JOIN
(SELECT * FROM table1 WHERE status="deleted") deleted
ON ok.name = deleted.name