简体   繁体   中英

How to use Sum and Inner Join in a delete statement and add a third table to it

This question is an addition to the following question answered here How to use Sum and Inner Join in a delete statement

I will need to add the following where statement WHERE oc_order_status.order_status_id IN ('3','5','17','19','20','23','25','26','29') to the following query:

Delete t
    from TablenName t join
         (select op.product_id, sum(op.quantity) as quantity
          from oc_order_product op
          group by op.product_id
         ) op
         on op.product_id = t.product_id and op.quantity < 2;

All tables share the same product_id column with same values and all tables are in the same database.

You can do a inner join of product and status table using the FK product_id and filter out the order_status_id in the where clause.

Delete t
    from TablenName t join
         (select op.product_id, sum(op.quantity) as quantity
          from oc_order_product op
          inner join oc_order_status os on os.product_id =op.product_id
          where os.order_status_id IN (3,5,17,19,20,23,25,26,29)
          group by op.product_id
         ) op
         on op.product_id = t.product_id and op.quantity < 2;

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