[英]How to insert combinations from 3 tables into a 4th table with 3 corresponding columns?
I have the following tables:我有以下表格:
table1: table2: table3:
| id | | id | | id |
|------| |------| |------|
| 1 | | 1 | | 1 |
| 2 | | 2 | | 2 |
How can I execute a MySql Query which inserts into a 4th table (table4) all possible combinations of the above tables which has according columns (id1, id2, id3)?如何执行 MySql 查询,该查询将上述表的所有可能组合插入到第 4 个表(表 4)中,这些表具有相应的列(id1、id2、id3)?
Basically I want my table4 to look like this:基本上我希望我的 table4 看起来像这样:
| id1 | id2 | id3 |
|-----|-----|-----|
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 1 |
| 1 | 2 | 2 |
| 2 | 1 | 1 |
| 2 | 1 | 2 |
| 2 | 2 | 1 |
| 2 | 2 | 2 |
You would use create table as
and cross join
:您将使用
create table as
和cross join
:
create table table4 as
select t1.id as id1, t2.id as id2, t3.id as id3
from table1 t1 cross join
table2 t2 cross join
table3 t3;
Note that SQL tables represent unordered sets.请注意,SQL 表表示无序集。 So, if you want to see the results in the order you have specified, then use:
因此,如果您想按照指定的顺序查看结果,请使用:
select t4.*
from table4 t4
order by id1, id2, id3;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.