6

I have a table say table1(id, col2, col3), and I want to duplicate all data of id 1 but with different id say 11 (id is not auto generated column). I wrote the following sql query which is not working for me (giving syntax error):

INSERT INTO table1(
id,
col2,
col3
)
VALUES (

SELECT 11 , col2, col3
FROM table1 WHERE id=1
)
4
  • And in what way has this not worked? What error did you get? Commented Sep 3, 2012 at 11:11
  • #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax Commented Sep 3, 2012 at 11:13
  • The problem is : don't put the "VALUES" keyword" when using insert... select Commented Sep 3, 2012 at 11:16
  • removed the VALUES keyword and put the id condition but still not working. Now getting the error #1054 - Unknown column 'col2' in 'field list' Commented Sep 3, 2012 at 11:22

4 Answers 4

10

Don't use the "VALUES" keyword

INSERT INTO table1(
id,
col2,
col3
)


SELECT 11 , col2, col3
FROM table1
WHERE id = 1

EDIT :

Check if you're working with the right column names :

DESC table1;
Sign up to request clarification or add additional context in comments.

6 Comments

+1, the only one that mentioned you need the where clause of id = 1 to make this work.
removed the VALUES keyword but still not working. Now getting the error #1054 - Unknown column 'col2' in 'field list'
@gmuhammad what happens if you write select 11, col2, col3 from table1 ?
getting the same error #1054 - Unknown column 'col2' in 'field list'
@gmuhammad then what gives DESC table1;
|
3

Try this:

INSERT INTO table1(
id,
col2,
col3
)
SELECT 11 , col2, col3
FROM table1
WHERE table1.id = 1

OR if you need more something like this:

INSERT INTO table1(id, col2, col3)
SELECT (SELECT MAX(id) FROM table1) + 1 , col2, col3
FROM table1

Comments

2

Use INSERT INTO ... SELECT with WHERE id = 1:

INSERT INTO table1(id, col2, col3)
SELECT '1' + id , col2, col3
FROM table1
WHERE id = 1

Comments

2

INSERT takes VALUES or SELECT, not both.

INSERT INTO table1( id, col2, col3 ) 
SELECT 11 , col2, col3 
FROM table1 
WHERE id = 1 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.