Mysql 使用select查询从两个没有公共列要联接的表中插入查询
这可能是一个简单的问题,但我不知道如何为此编写sql查询 我有以下情况: 表1:Mysql 使用select查询从两个没有公共列要联接的表中插入查询,mysql,sql,oracle,Mysql,Sql,Oracle,这可能是一个简单的问题,但我不知道如何为此编写sql查询 我有以下情况: 表1: id name 1 aaaa 2 bbbb 3 cccc 4 dddd 表2: id name 1 xxxx 2 yyyy 输出表-表1和表2之间的映射表: id Table1_id Table2_id 1 1 1 2 1 2 3 2
id name
1 aaaa
2 bbbb
3 cccc
4 dddd
表2:
id name
1 xxxx
2 yyyy
输出表-表1和表2之间的映射表:
id Table1_id Table2_id
1 1 1
2 1 2
3 2 1
4 2 2
5 3 1
6 3 2
7 4 1
8 4 2
我想为上面的输出表编写插入查询
如下所示
insert into output_table(table1_id, table2_id)
select table1.table1_id, ....
from table1 .... ?
请注意,这两个表之间没有连接列,对于表1中的每个id,都应该与id的1和2映射,并且在输出表中应该有一个条目
有人能帮我写这个查询吗
提前感谢。使用交叉连接。这将在两个表之间生成笛卡尔积,这似乎就是您要寻找的
INSERT INTO output_table(table1_id, table2_id)
SELECT t1.id, t2.id
FROM table1 t1
CROSS JOIN table2 t2
这也可以使用隐式连接拼写如下:
INSERT INTO output_table(table1_id, table2_id)
SELECT t1.id, t2.id
FROM table1 t1, table2 t2
感谢@GMB的快速响应。如果我在表1中有where子句,我应该如何应用它?@user10566909:where子句位于查询的末尾、FROM子句之后和JOIN之后。