Mysql从所有表中选择两列

时间:2018-12-12 17:29:06

标签: mysql sql postgresql mysqli postgresql-9.3

这里没有相关表

表1:

id | title   | amount | 
-----------------------
 1 | Task1   | 30     | 
 2 | Task2   | 20     | 
 3 | Task2   | 40     | 
 4 | Task2   | 10     | 
 5 | Task2   | 30     | 

表2:

id | name   | total | 
---------------------
 7  | name1  | 30    | 
 8  | name2  | 20    | 
 9  | name3  | 40    | 
 10 | name4  | 10    | 
 11 | name5  | 30    | 

表3:

id | name   | total_count | 
---------------------------
 20 | name1  | 10          | 
 21 | name2  | 22          | 
 22 | name3  | 33          | 
 23 | name4  | 23          | 
 24 | name5  | 42          |
 25 | name6  | 90          |
 26 | name7  | 85          |

从每两列的表格中选择一个,请帮助,对不起我的英语不好,有人帮助,我需要得到以下结果:

结果:

t1_title | t1_amount | t2_name | t2_total | t3_name | t3_total |
----------------------------------------------------------------
 Task1   | 30        | name1   | 30       | name1   | 10       |
 Task2   | 20        | name2   | 20       | name2   | 22       |
 Task3   | 40        | name3   | 40       | name3   | 33       |
 Task4   | 10        | name4   | 10       | name4   | 23       |
 Task5   | 30        | name5   | 30       | name5   | 42       |
         | 0         |         | 0        | name6   | 90       |
         | 0         |         | 0        | name7   | 87       |

在表之间没有相关(关系)。

谢谢!

1 个答案:

答案 0 :(得分:1)

您似乎希望在三个表之间有一个FULL OUTER JOIN,我相信这需要使用三个查询的UNION来实现,例如:

select t1.title, t1.amount, t2.name, t2.total, t3.name, t3.total_count
from t1 left join t2 on t1.id = t2.id left join t3 on t1.id = t3.id
union 
select t1.title, t1.amount, t2.name, t2.total, t3.name, t3.total_count
from t2 left join t1 on t2.id = t1.id left join t3 on t2.id = t3.id
union
select t1.title, t1.amount, t2.name, t2.total, t3.name, t3.total_count
from t3 left join t1 on t3.id = t1.id left join t2 on t3.id = t2.id