I would like to know if I can run a request like that:
INSERT INTO t2 (a, b) VALUES ( SELECT a, b FROM `t1` AS o WHERE o.id NOT IN ( SELECT a FROM t2 ) )
The idea is to fill the t2 with some data from the t1, but I must be wrong on the syntax.
Thanks for your help
The MySQL INSERT INTO SELECT Statement The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables matches. Note: The existing records in the target table are unaffected.
You can use a select-statement within an INSERT statement to insert zero, one, or more rows into a table from the result table of the select-statement. The select-statement embedded in the INSERT statement is no different from the select-statement you use to retrieve data.
6 Answers. Show activity on this post. INSERT just returns true or false. to actually return something useful, you will need a SELECT or similar query.
You don't use the VALUES
keyword when inserting from a SELECT
statement.
INSERT INTO t2 (a, b) SELECT a, b FROM `t1` AS o WHERE o.id NOT IN ( SELECT a FROM t2 )
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