如何“插入到表(col1,col2)值中(从table2中选择max(id),从table3中选择id); &quot ;? [英] how to "insert into table (col1, col2) values (select max(id) from table2, select id from table3); "?

查看:76
本文介绍了如何“插入到表(col1,col2)值中(从table2中选择max(id),从table3中选择id); &quot ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用称为table1_table2的查找表在table1中的max(id)与table2中的所有ID之间建立多对多关系.

I am trying to create a many-many relationship between the max(id) in table1 with all the ids in table2, using a lookup table called table1_table2.

最终使table1_table2中的行为:

Ultimately so that the rows in table1_table2 will be:

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; 

但似乎都不起作用

推荐答案

听起来这就是您想要的:

It sounds like this is what you want:

INSERT INTO table1_table2 (table1_id, table2_id) 
    SELECT MAX(table1.id), table2.id FROM table1, table2 GROUP BY table2.id;

这篇关于如何“插入到表(col1,col2)值中(从table2中选择max(id),从table3中选择id); &quot ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆