MySQL错误:尝试插入值时,子查询返回的行数超过1行

时间:2014-11-11 00:42:42

标签: mysql opencart

我有以下MySQL脚本。我想做的是:

  • 获得100种畅销产品
  • 获取价格
  • 计算价格20%的折扣
  • 然后将价格插入特价表

当我将子查询的限制更改为 1 时,这很有效,但是如何在一个脚本中为所有100个产品执行此操作?

以下是我收到的错误:Subquery returns more than 1 row

INSERT INTO oc_product_special (product_id, customer_group_id, priority, price, date_start, date_end)
VALUES (

(SELECT op.product_id
FROM oc_order_product op 
LEFT JOIN oc_order o 
ON (op.order_id = o.order_id) 
LEFT JOIN oc_product p ON (op.product_id = p.product_id) 
LEFT JOIN oc_product_to_store p2s ON (p.product_id = p2s.product_id) 
WHERE o.order_status_id > '0' 
AND p.status = '1' 
AND p.date_available <= NOW() 
AND p2s.store_id = 0 
GROUP BY op.product_id 
ORDER BY COUNT(*) DESC 
LIMIT 100),

"1","20",

(SELECT p.price * 0.8 AS "price" 
FROM oc_order_product op 
LEFT JOIN oc_order o 
ON (op.order_id = o.order_id) 
LEFT JOIN oc_product p ON (op.product_id = p.product_id) 
LEFT JOIN oc_product_to_store p2s ON (p.product_id = p2s.product_id) 
WHERE o.order_status_id > '0' 
AND p.status = '1' 
AND p.date_available <= NOW() 
AND p2s.store_id = 0 
GROUP BY op.product_id 
ORDER BY COUNT(*) DESC 
LIMIT 100),

"2014-11-11","2030-01-01"
)

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

如果要插入VALUES的所有结果,请勿使用SELECT。只需使用SELECT

即可
INSERT INTO oc_product_special (product_id, customer_group_id, priority, price, date_start, date_end)
SELECT op.product_id, "1", "20", p.price * 0.8, "2014-11-11", "2030-01-01"
FROM oc_order_product op 
LEFT JOIN oc_order o 
ON (op.order_id = o.order_id) 
LEFT JOIN oc_product p ON (op.product_id = p.product_id) 
LEFT JOIN oc_product_to_store p2s ON (p.product_id = p2s.product_id) 
WHERE o.order_status_id > '0' 
AND p.status = '1' 
AND p.date_available <= NOW() 
AND p2s.store_id = 0 
GROUP BY op.product_id 
ORDER BY COUNT(*) DESC 
LIMIT 100

答案 1 :(得分:0)

格式错误,要插入多条记录,您应该执行以下操作:

INSERT INTO SomeTable ( Col1, Col2, Col3 )
SELECT Val1, Val2, Val3 FROM SomeOtherTable