我有以下MySQL脚本。我想做的是:
当我将子查询的限制更改为 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"
)
感谢您的帮助。
答案 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