2017-05-08 74 views
0

我有一個表稱爲TableName我想delete所有行,他們的產品質量的SUM小於2 我需要inner joinoc_order_productSUM值具有相同product_id然後使用WHERE子句中這SUMdelete所有行與SUM小於2 我使用下面的查詢現在:如何使用和與內部連接在一個delete語句

Delete TablenName from TablenName 
     INNER JOIN oc_order_product 
        ON oc_order_product.product_id = TablenName.product_id 
    where oc_order_product.quantity HAVING SUM(oc_order_product.quantity) < 2; 

對此我收到以下錯誤:

#1064 - You have an error in your SQL syntax; check the manual that 
corresponds to your MariaDB server version for the right syntax 
to use near 'HAVING SUM(oc_order_product.quantity) < 2' at line 4 

回答

2

嗯。 。 。如果你需要做的的聚集,需要join之前做到這一點:

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

謝謝你,你也可以告訴我,如果我可以添加'WHERE oc_order_status.order_status_id IN(「3」,「5」,」 17','19','20','23','25','26','29')'。對不起,我剛纔發現我也需要這個,如果你願意,我可以將它添加到問題中。 – Schwann

+0

@Swann。 。 。因爲這涉及第三個表格,所以你應該提出另一個問題。 –

+0

我沒有編輯這個問題,並通過以下鏈接發佈了一個新問題[http://stackoverflow.com/questions/43861288/how-to-use-sum-and-inner-join-in-a-delete-聲明和添加一個第三表到它],我將不勝感激那一個答案。 – Schwann