mysql insert where data from 结合两个 table 没有关系

mysql insert where data from combine two table with no relation

我有两个table
Table1

NO | CITY  
1  | city1 
2  | city2

表 2

NO | PRODUCT  | CODE
1  | product1 | 1
2  | product2 | 2
3  | product3 | 1

我想合并 table1 和 table2,其中 table2 代码为 1,然后像这样将其插入 table3
表 3

NO | CITY  | PRODUCT
1  | city1 | product1
2  | city1 | product3
3  | city2 | product1
4  | city2 | product3

感谢您的帮助。

如果你的NOAUTO_INCREMENT列,我们可以尝试使用INSERT INTO ... SELECTCROSS JOIN 来做到这一点。

INSERT INTO Table3 (CITY  ,PRODUCT)
SELECT t1.CITY  ,t2.PRODUCT
FROM Table1 t1
CROSS JOIN Table2 t2
WHERE t2.CODE = 1