开发者

MySQL insert if condition met in another table

开发者 https://www.devze.com 2023-01-24 11:37 出处:网络
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:

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消