0

我有一张桌子,三列是这样的:

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
--------------------------------------------*/
4

1 回答 1

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
于 2013-04-10T19:24:37.673 回答