如何插入表(col1,col2)的值(从表2中选择max(id),从表3中选择id);?
技术问答
235 人阅读
|
0 人回复
|
2023-09-12
|
我试着用它table1_table2的查找表在table1中的max(id)与table2中的所有id创建多对多关系。" f2 p. N$ F( i; U4 c4 V. D
最终,使table1_table2中的行为:
; u6 R( g4 Q, Y w: O5 g) M% a: mtable1_id,table2_id30,130,230,3...30,10000我怎样才能做到这一点?
7 e% _" D% M! l5 s& c4 q9 S我试过了
. q3 G h2 b9 J) \" G* m5 c. tinsert into insert into table1_table2 (table1_id,table2_id) values (select max(id) from table2,select id from table3);和
. {3 d/ _' z1 d+ B# sinsert into insert into table1_table2 (table1_id,table2_id) select max(table1_id),table2_id from table join table1_table2 on table1_table2.table1_id = table1.id outer join table1_table2 on table1_table2.table2_id = table2.id;但似乎都不起作用
) k7 d, t; ^4 k) f 1 B7 ~" L( x$ {7 h2 ]; [+ c
解决方案:
" H5 O3 s6 T' ?8 U- h 听起来这就是你想要的:
3 x. _9 j! \5 c) J- k3 sINSERT INTO table1_table2 (table1_id,table2_id) SELECT MAX(table1.id),table2.id FROM table1,table2 GROUP BY table2.id; |
|
|
|
|
|