INSERT into with filter in MySQL
I'm trying to create an INSERT INTO in a table X by making a filter in Table Y, but I get a syntax error. I researched what might be wrong, but didn't find out.
INSERT INTO products (
SELECT * FROM products AS P
WHERE P.FK_ID_QUOTE = 101
)
2
Author: Leonardo Vinicius, 2018-11-27
2 answers
Where you are placing the query you have to specify the columns you want to copy and then comes the values that can be obtained by the selection, something like this:
INSERT INTO products (nome, valor, etc)
SELECT nome, valor, etc FROM products AS P
WHERE P.FK_ID_QUOTE = 101
4
Author: Maniero, 2018-11-27 16:13:13
The solution would be to make a subquery, as below:
I created the person table_2, which I want to insert data into it as the person Table:
In this case, the query to insert in PERSON_2 would be:
INSERT INTO PESSOA_2 (IDADE, ID_PESSOA, NOME, DATA_NASC)
SELECT 15, ID_PESSOA, NOME, DATA_NASC FROM PESSOA WHERE IDADE = 20;
The result:
2
Author: Elisiany Oliveira, 2018-11-27 13:25:08