为每个选定的记录插入记录

时间:2019-11-04 12:49:02

标签: mysql sql

我有两个表,Table1包含具有列super_iduser_namejob_type的记录列表

Table2包含3列super_idviewtime

使用带有查询条件的表上的选择查询,我想在super_id中为每个Table2创建一条记录

表示选择查询是否为SELECT super_id FROM Table1 WHERE job_type = “Instructor”

RF34323RF34328会出现一次,每个都会插入一次到Table2中,其中View列始终是View1,时间是当前日期。 / p>

如何编写这样的Select Insert查询?

以下是两个表格的示例:

Example of Tables

1 个答案:

答案 0 :(得分:0)

这是您想要的吗?

insert into table2 (super_id, `view`, `time`)
    select super_id, 'view1', now()
    from table1
    where job_type = 'Instructor';

请注意,viewtime是列名的非常不好的选择,因为它们是SQL中的关键字。

如果要创建table2,请使用create table table2 as代替insert

此外,如果正确设置表格,则可以将time列默认为插入时间。

相关问题