MySQL插入表中,值形成另一个表

时间:2021-01-11 22:43:38

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

我的MySQL数据库中有一个表table_a。 (我使用PHP编写脚本)

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).

现在我在实现其必要性后创建了另一个表,称为table_b。对于table_a中的每一行,我想将其一些值插入table_b,然后加上时间戳(DATETIME类型)。

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).

其中$ datetime是一个时间值(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.

提前致谢。

1 个解决方案

#1


1  

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.

这会将table_a的每一行中的所需值添加到table_b中。

#1


1  

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.

这会将table_a的每一行中的所需值添加到table_b中。