mysql - 从 MySQL 中的 select 中删除

标签 mysql sql

我正在尝试从以下 SELECT 查询中删除结果行

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 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)

最佳答案

您无法在删除语句中从要删除的表中进行选择 - 这就像在迭代集合时尝试从集合中添加/删除项目一样。

这是 MySQL docs 的官方声明:

Subqueries

You cannot delete from a table and select from the same table in a subquery.

您需要使用 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

关于mysql - 从 MySQL 中的 select 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42031742/

相关文章:

python - 启动一个 "throwaway"MySQL session 来测试代码?

sql - Django过滤与用户组具有一对一关系的项目

sql - MySQL数据库设计问题

java - ORA-00932 : inconsistent datatypes: expected DATE got BINARY in Hibernate

mysql - 限制用户的服务器资源时出错

java - hibernate 。优化 sqlRestriction

mysql - 使用 SQL 数据库查询以不同的时间间隔减去相同的列值

c# - 食品营养信息

php - 将 Ajax 与 PHP 和 MySQL 结合使用

PHP OOP MySQL 编程