首页 > 解决方案 > 交叉引用表并将结果插入另一个表 SQL

问题描述

所以我有 3 个表,如下所示:

表 1 - 猫 - 具有以下结构:

  id   name       plural_name 
   0   painters   painters
   1   builders   builders

表 2 - 地方 - 具有以下结构

       place_id   place_name       classification
       243        painter comp     painters
       230        builder comp     builders

表 3 - rel_place_cat - 具有以下结构

      id    place_id    cat_id 

        0   243         0
        1   230         1

所以基本上我需要运行一个 SQL 命令来查找表 1 中的类别 ID 号和表 2 中的 place_id 并将数据插入表 3

这可以做到吗?

谢谢

标签: phpsql

解决方案


就像是:

insert into table3(place_id, cat_id) select place_id, cat_id from(select place_id, cat_id from table1 inner join table2 on table1.name = table 2.classification)

应该管用。


推荐阅读