Today is the intermediate tutorial on MySQL INSERT statements (hence the 202). If you need to review the previous tutorial, you can check that out here.
Last time, we did an INSERT of a single row into a table, but what if we wanted to do several?
That will involve some changing syntax. That syntax will look like this:
INSERT INTO table2 (col1, col2) SELECT col1, col2 FROM table1 WHERE 1=1;
Now with this, we could insert more than one row into a table at a time. This can be proven to be very useful when you have to do a bulk insert into a table.
This combines a SELECT statement in order to get 1 or more rows from table1 and then an INSERT statement to put those rows into table2. I have found this to be very useful when writing programs that will be run many times (crons mostly).
I know that this post is quite small, but next level (302) we will be going into how exactly the INSERT statement works on the database, so stay tuned!
That’s all for now, until next time…keep that tech gear ticking.
~Goliath7470
