Search code examples
mysqlsqlinsert-intoinsert-select

Insert Into query broken in mysql


I need to run this kind of query.

INSERT INTO `book_newBookOrder` (id, volume, 5, chapterNr, sectionNr, `text`) 
SELECT id, volume, bookNr, chapterNr, sectionNr, `text`
FROM book_oldBookOrder 
WHERE booknr = 1;

The fixed value 5 in the INSERT INTO part breaks it. I must be able to specify the two values as above.

So I want to select everything with bookNr = 1 in the oldbooknr table and store that as booknr 5 in the newbookorder table.

Please advise me. Thanks.


Solution

  • You have a syntax error: The items in the first set of brackets in the INSERT should be the field names. "5" is not a field name, that's the value you want to be inserted (I assume you wish to set this value the same in every row which gets inserted?). That should be in the SELECT:

    INSERT INTO `book_newBookOrder` (id, volume, bookNr, chapterNr, sectionNr, `text`) 
    SELECT id, volume, 5, chapterNr, sectionNr, `text`
    FROM book_oldBookOrder 
    WHERE booknr = 1;