我正在尝试使用名为table1_table2的查找表在table1中的max(id)与table2中的所有id之间创建多对多的关系。
最终使table1_table2中的行为:
table1_id, table2_id
30, 1
30, 2
30, 3
...
30, 10000
我该怎么做?
我试过了
insert into insert into table1_table2 (table1_id, table2_id)
values (select max(id) from table2, select id from table3);
和
insert into insert into table1_table2 (table1_id, table2_id)
select max(table1_id), table2_id from table1
join table1_table2 on table1_table2.table1_id = table1.id
outer join table1_table2 on table1_table2.table2_id = table2.id;
但似乎都不起作用
答案 0 :(得分:3)
我看到你有3张桌子。我想你的意思是:
insert into table1_table2 (table1_id, table2_id)
SELECT
(select max(id) from table2) --- or perhaps: from table1 ?
, id
FROM table3 --- or perhaps: FROM table2 ?
答案 1 :(得分:3)
听起来这就是你想要的:
INSERT INTO table1_table2 (table1_id, table2_id)
SELECT MAX(table1.id), table2.id FROM table1, table2 GROUP BY table2.id;