我需要连接4个表以获取所需的报表。但是我不知道如何编写查询。下面是这些表的示例:
客户表
client_id | client_name | con_id
----------:|:-------------:|:-------
1 | ABC | 1
2 | DEF | 1
3 | GHI | 2
顾问
con_id | con_name
-------:|:---------
1 | Ani
2 | Robby
永久
pid | client_id | date
----:|:-----------:|:-----------
1 | 1 | 2014-08-09
2 | 1 | 2014-03-02
3 | 2 | 2014-03-02
温度
tid | client_id | date
----:|:-----------:|:-----------
1 | 2 | 2013-02-09
2 | 3 | 2011-03-02
3 | 3 | 2012-04-02
我想展示的报告的最终结果是这样的:
client_id | client_name | perm(COUNT) | temp(COUNT) | con_name
----------:|:-------------:|:-------------:|:-------------:|:---------
1 | ABC | 2 | 0 | Ani
2 | DEF | 1 | 1 | Ani
3 | GHI | 0 | 2 | Robby
我正在尝试使用LEFT OUTER JOIN
,但是没有得到想要的结果。有人能帮我弄清楚这个查询吗?
3条答案
按热度按时间vktxenjb1#
这是一个带有count和group by的简单外部连接查询,只需将
client
表与相关表连接起来,并仅计算非重复关联Fiddle Demo
(第一次)2ul0zpep2#
您应该通过在一个sql中连接所有查询来连接所有表。请参见链接http://www.w3resource.com/sql/joins/using-a-where-cluase-to-join-two-tables-related-by-a-single-column-primary-key-or-foriegn-key-pair.php
dly7yett3#
....您要选择的内容....