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
)
To select duplicate values, you need to create groups of rows with the same values and then select the groups with counts greater than one. You can achieve that by using GROUP BY and a HAVING clause.
Find duplicate values in one column First, use the GROUP BY clause to group all rows by the target column, which is the column that you want to check duplicate. Then, use the COUNT() function in the HAVING clause to check if any group have more than 1 element. These groups are duplicate.
The Insert on Duplicate Key Update statement is the extension of the INSERT statement in MySQL. When we specify the ON DUPLICATE KEY UPDATE clause in a SQL statement and a row would cause duplicate error value in a UNIQUE or PRIMARY KEY index column, then updation of the existing row occurs.
From the Query Designer menu, point to Change Type, and then click Insert Results. In the Choose Target Table for Insert Results Dialog Box, select the table to copy rows to (the destination table).
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;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With