user485498
user485498

Reputation:

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.

Upvotes: 1

Views: 1182

Answers (1)

user485498
user485498

Reputation:

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.

Upvotes: 1

Related Questions