简体   繁体   English

如何将 3 个表的组合插入具有 3 个对应列的第 4 个表中?

[英]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 ascross 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.

相关问题 如何在第 4 列包含与另一个表相同的 ID 的表中显示 3 列中的所有数据? - How to display all data in 3 columns from a table where the 4th column contains the same ID as another table? 如何使用 Flask SQLAlchemy 中第 4 个表中的键查询 4 ​​个表? - How to Query 4 tables with the key in the 4th table in Flask SQLAlchemy? PHP加入3个表,然后插入到第4 - php join 3 tables then insert to 4th SQL根据某些条件将数据从3个表合并到第4个表 - SQL Combining data from 3 tables to from a 4th table based on some conditions SQL 连接 3 个表,然后在单个查询中从第 4 个表中提取数据 - SQL joins 3 tables and then pull data from 4th table in single query PDO:无法从MySQL的第4个表中提取数据 - PDO: Can't pull data from a 4th table in MySQL 来自txt(csv)文件的Python图形,共有5列,如何为y轴选择第3列或第4列? - Python graph from txt(csv) file and there are 5 columns, How Can I pick 3rd or 4th columns for y axis? 我在MySQL中有4个表-与没有重复的第4个表联接(可能为null值) - I have 4 tables in MySQL - Join with 4th table without duplicates (possible null value) 从一个表中选择多个列,从另一个表中获取相应的详细信息并将它们插入到另一个表中 - Select multiple columns from one table, get corresponding details from another table and insert these to another table 来自两个不同表的两列的组合 - Combinations for two columns from two different tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM