2017-02-03 48 views
0

我想從以下SELECT查詢刪除導致的行刪除從選擇在MySQL

select customer_ID, order_ID, t1, t2 
from web_order, items_table as a, items_table as b 
    where (web_order.t1 = a.item_name and a.quantity) 
    and (web_order.t2 = b.item_name and b.quantity) 
    and ((a.quantity < b.quantity)); 

這是我的DELETE查詢

delete from web_order where customer_ID in 
(select customer_ID, order_ID, t1, t2 
from web_order, items_table as a, items_table as b 
    where (web_order.t1 = a.item_name and a.quantity) 
    and (web_order.t2 = b.item_name and b.quantity) 
    and ((a.quantity < b.quantity))); 

但我得到這個錯誤

ERROR 1241 (21000): Operand should contain 1 column(s) 
+1

你想擺脫行的其中'customer_ID' - 一列--'is in'包含四列的查詢。不要這樣做。 – 2017-02-03 20:22:32

回答

2

你不能從刪除語句中刪除的表中選擇 - 這就像試圖添加/刪除它當你迭代它時,從一個集合中獲取ems。

下面是來自MySQL docs官方聲明:

子查詢

你不能從表中刪除,並在 子查詢中從同一個表中選擇。

您將需要使用您的select語句將customer_ID值插入臨時表中,然後從刪除中的臨時表中進行選擇。

所以,它會是這個樣子(我假設CUSTOMER_ID是在你的web_order表中的INT列):

CREATE TEMPORARY TABLE CustomerIDs (Customer_ID INT NOT NULL) 

INSERT INTO CustomerIDs 
select customer_ID from web_order, items_table as a, items_table as b 
where (web_order.t1 = a.item_name and a.quantity) 
and (web_order.t2 = b.item_name and b.quantity) 
and ((a.quantity < b.quantity)) 

DELETE FROM web_order WHERE Customer_ID IN (SELECT Customer_ID FROM CustomerIDs) 

DROP TABLE CustomerIDs 
+0

它的工作原理!非常感謝你,我非常感謝你的幫助 – user6872853