简体   繁体   中英

MySQL insert into table, values form another table

I have a table, table_a, in my MySQL db. (I am using PHP for scripting)

Now I have created another table, after realizing its necessity, called table_b. For every row in table_a I want to insert some of its values into table_b, and then affix a timestamp (DATETIME type).

This is where I am:

$query = "INSERT INTO table_b('id_a', 'type_a', 'date_a') SELECT table_a.id, table_a.type, '$datetime'";

where $datetime is a time value (php).

I'm not sure this is going to work. Could someone tell me a correct way to do this.

(Aside: I am aware I'm not using prepared statements - that's for another day)

Thanks in advance.

With thanks to all the comments, the answer is simple:

$query = "INSERT INTO table_b(id_a, type_a, date_a) SELECT table_a.id, table_a.type, '$datetime' FROM table_a";

This adds the desired values from every row of table_a into table_b.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM