How to combine multiple tables with different columns

♀尐吖头ヾ 提交于 2020-12-13 06:31:22

问题


I have 3 tables. I want the school table to set the data of major_subject and get the minor_subject on the result.

And also set automatically units 3.0 for major_subject since this table has no column.

I tried to use UNION but I get error since major_subject column is not the same with minor_subject table.

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  |    

回答1:


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:

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 


来源:https://stackoverflow.com/questions/65248274/how-to-combine-multiple-tables-with-different-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!