Helpful ways to nuance an INSERT statement
Like SELECT
has other helpful quantifiers to weed through the data being returned, INSERT
has ways of nuancing the origin of the data to be inserted as well as the timing and conditions of the insertion. The three most common ways of altering the way MySQL processes an INSERT
statement are:
INSERT...SELECT...
INSERT DELAYED...
INSERT...ON DUPLICATE KEY UPDATE...
In the following section, we take each one in turn.
INSERT...SELECT...
Using INSERT...SELECT...
we can tell MySQL to draw from different tables without having to draw them into Python or to set a variable in MySQL. It functions on the following syntactic template:
INSERT INTO <target table>(target column name) SELECT <source column name> FROM <source table>;
By default, the SELECT
phrase of the sentence is greedy and will return as many hits as it can. As with a generic SELECT
statement, however, we can restrict the hits returned using WHERE
. See the Other helpful quantifiers section...