简体   繁体   中英

Subtract one row of two results

I have one table with three columns like this:

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

I want result like this (is it possible ?):

# Result3 = (Result 1) - (Result 2)
/*--------------------------------------------
    qnt     name
    8       prod1
    7       prod2
--------------------------------------------*/
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM