MySQL insert if condition met in another table
Im looking 开发者_如何学编程for the appropriate way to insert date into one table based on the value of a field in another table.
You need to use the INSERT..SELECT
syntax:
INSERT INTO first_table (...)
SELECT somevalue FROM second_table WHERE condition;
The SELECT
fields must match the INSERT
fields. You can add constant columns in the SELECT
too:
SELECT somecolumn, 'constant value' ...
to supply values not available in second_table
.
there are many ways, e.g. insert into mytable1 (date_field) values(select date_field from mytable2 where pkfield=16)
精彩评论