简体   繁体   English

如何将多个表与不同的列组合起来

[英]How to combine multiple tables with different columns

I have 3 tables.我有 3 张桌子。 I want the school table to set the data of major_subject and get the minor_subject on the result.我希望school表设置major_subject的数据并得到minor_subject的结果。

And also set automatically units 3.0 for major_subject since this table has no column.并且还自动为major_subject设置units 3.0 ,因为该表没有列。

I tried to use UNION but I get error since major_subject column is not the same with minor_subject table.我尝试使用UNION但我收到错误,因为major_subject列与minor_subject表不同。

school                                      major_subject     

| school_id |  school_name  |             |   subj_name    |  date_offered |
-----------------------------             ---------------------------------
|   1       |    schoolA    |             |  Business101   |   2021/01/01  |
|   2       |    schoolB    |             |  Marketing101  |   2021/01/01  |
|   3       |    schoolC    |        


minor_subject

| school_id  |  subj_name  | units  | date_offered |      
----------------------------------------------------
|     1      |   Math      |  1.0   |  2021/01/01  |     
|     1      |   English   |  1.0   |  2021/01/01  | 
|     1      |   Science   |  1.0   |  2021/01/01  | 
|     2      |   History   |  2.0   |  2021/01/01  | 

And the result table would be like:结果表如下:

| school_id  |  subj_name     |  units   | date_offered |      
---------------------------------------------------------         
|     1      |  Business101   |   3.0    |  2021/01/01  |     
|     1      |  Marketing101  |   3.0    |  2021/01/01  |    
|     1      |  Math          |   1.0    |  2021/01/01  |     
|     1      |  English       |   1.0    |  2021/01/01  | 
|     1      |  Science       |   1.0    |  2021/01/01  | 

|     2      |  Business101   |   3.0    |  2021/01/01  |     
|     2      |  Marketing101  |   3.0    |  2021/01/01  |    
|     2      |  History       |   2.0    |  2021/01/01  |     

|     3      |  Business101   |   3.0    |  2021/01/01  |     
|     3      |  Marketing101  |   3.0    |  2021/01/01  |    

You can CROSS join school to major_subject to get all the combinations of the rows of the 2 tables and then with UNION ALL add the rows from minor_subject :您可以将school CROSS加入major_subject以获取 2 个表的行的所有组合,然后使用UNION ALL添加来自minor_subject的行:

SELECT s.school_id, m.subj_name, 3.0 units, m.date_offered 
FROM school s CROSS JOIN major_subject m
UNION ALL
SELECT school_id, subj_name, units, date_offered 
FROM minor_subject 
ORDER BY school_id

See the demo .请参阅演示
Results:结果:

> school_id | subj_name    | units | date_offered
> --------: | :----------- | ----: | :-----------
>         1 | Business101  |   3.0 | 2021-01-01  
>         1 | Marketing101 |   3.0 | 2021-01-01  
>         1 | Math         |   1.0 | 2021-01-01  
>         1 | English      |   1.0 | 2021-01-01  
>         1 | Science      |   1.0 | 2021-01-01  
>         2 | History      |   2.0 | 2021-01-01  
>         2 | Marketing101 |   3.0 | 2021-01-01  
>         2 | Business101  |   3.0 | 2021-01-01  
>         3 | Business101  |   3.0 | 2021-01-01  
>         3 | Marketing101 |   3.0 | 2021-01-01 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将两个不同的表与不同的列组合 - How to combine two different tables with different columns 如何组合列中略有不同的多个表 - How to combine multiple tables that vary slightly in columns 合并具有不同列数的表 - Combine tables with different number of columns 如何将三个不同表中的不同列合并到一张表中? - How to combine different columns from three different tables into one table? SQL:如何将来自多个表的计数结果合并到多个列中 - SQL: How to combine count results from multiple tables into multiple columns 如何合并两个列数不同的表SQL - How to combine two tables with different number of columns SQL 如何在一个表中合并来自不同表的单独列 - How to combine separate columns from different tables in a single table 如何组合2个表的行,这些表具有相同和不同的列 - How to combine rows of 2 tables having some columns same and some different 如何将来自不同列的多个列组合到一个表或视图中? - How to combine multiple columns from different as columns in to one table or view? 将具有多个列/名称数量的表的多个select *查询组合在一起,以导出到另一个数据库中 - Combine multiple select * queries for tables with a different number of columns/names for export into a different database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM