MySQL 插入来自 select 的值
MySQL insert with value from select
我有这个查询:
INSERT INTO oc_stock_hpp (product_id, buy_price)
VALUES (
'3337',
(SELECT buy_price
FROM oc_stock_hpp
WHERE product_id ='3337'
ORDER BY id DESC
LIMIT 1)
)
我收到一个错误:
Table 'oc_stock_hpp' is specified twice, both as a target for 'INSERT' and as a separate source for data
我该怎么办?
您可以使用 insert-select 语句并将 product_id 添加到查询中:
INSERT INTO oc_stock_hpp (product_id, buy_price)
SELECT product_id, buy_price
FROM oc_stock_hpp
WHERE product_id = '3337'
ORDER BY id DESC LIMIT 1
我有这个查询:
INSERT INTO oc_stock_hpp (product_id, buy_price)
VALUES (
'3337',
(SELECT buy_price
FROM oc_stock_hpp
WHERE product_id ='3337'
ORDER BY id DESC
LIMIT 1)
)
我收到一个错误:
Table 'oc_stock_hpp' is specified twice, both as a target for 'INSERT' and as a separate source for data
我该怎么办?
您可以使用 insert-select 语句并将 product_id 添加到查询中:
INSERT INTO oc_stock_hpp (product_id, buy_price)
SELECT product_id, buy_price
FROM oc_stock_hpp
WHERE product_id = '3337'
ORDER BY id DESC LIMIT 1