MySQL INSERT INTO表1(col1,col2,col3 ...)VALUE(SELECT xxx)不起作用

时间:2018-01-30 08:51:38

标签: mysql

我按照这个帖子来构建查询但是我的工作不起作用:MySQL INSERT INTO ... VALUES and SELECT这是我的查询:

INSERT INTO item_category (id,item_name,price ) VALUES
(SELECT id, item_name, price FROM item_info WHERE item_info.price >99);

这是我收到的错误消息:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT id, item_name, price FROM item_info WHERE item_info.price >99)' at line 2

3 个答案:

答案 0 :(得分:3)

从select查询插入表的语法不使用VALUES

INSERT INTO item_category (id, item_name, price)
SELECT id, item_name, price
FROM item_info
WHERE price > 99;

VALUES形式的insert保留用于插入文字元组,例如

INSERT INTO item_category (id, item_name, price)
VALUES
    (1, 'some item', 99);

请注意,我们还可以使用select:

表示上述值insert
INSERT INTO item_category (id, item_name, price)
SELECT 1, 'some item', 99
FROM dual;    -- or some other table

当我们想要将文字与实际列值混合时,这个技巧很有用。

答案 1 :(得分:0)

删除Values

INSERT INTO item_category (
    id
    ,item_name
    ,price
    )
SELECT id
    ,item_name
    ,price
FROM item_info
WHERE item_info.price > 99;

答案 2 :(得分:0)

删除VALUES并尝试

INSERT INTO item_category (id,item_name,price )
SELECT id, item_name, price FROM item_info WHERE item_info.price >99

会工作!!