INSERT INTO with filter in MySQL

1

I'm trying to create an INSERT INTO in an X table by making a filter in the Y table, but I get a syntax error. I researched what may be wrong, but not find out.

INSERT INTO products (
    SELECT * FROM products AS P
    WHERE P.FK_ID_QUOTE = 101
)
    
asked by anonymous 27.11.2018 / 14:03

2 answers

3

Where you are placing the query you have to specify the columns you want to copy and then the values that can be obtained by the selection come up, something like this:

INSERT INTO products (nome, valor, etc)
    SELECT nome, valor, etc FROM products AS P
    WHERE P.FK_ID_QUOTE = 101

I placed GitHub for future reference .

    
27.11.2018 / 14:18
2

The solution would be to make a subquery, as below:

I created the PESSOA_2 table, which I want to insert data into it according to the PERSON table:

Inthiscase,thequerytoenterinPERSON_2wouldbe:

INSERTINTOPESSOA_2(IDADE,ID_PESSOA,NOME,DATA_NASC)SELECT15,ID_PESSOA,NOME,DATA_NASCFROMPESSOAWHEREIDADE=20;

Theresult:

    
27.11.2018 / 14:25