在sql join中需要帮助才能从三个表中获取输出

时间:2015-10-01 10:05:34

标签: sql sql-server sql-server-2008 sql-server-2012

我有三张这样的表:

表格模块:

+----------+-------------+
| Moduleid | Module name |
+----------+-------------+
|        1 | m1          |
|        2 | m2          |
|        3 | m3          |
|        4 | m4          |
+----------+-------------+

表:问题

+-----------+----------+
| problemid | moduleid |
+-----------+----------+
| p1        | m1       |
| p2        | m1       |
| p3        | m2       |
| p4        | m2       |
| p5        | m3       |
| p6        | m3       |
| p7        | m4       |
| p8        | m4       |
+-----------+----------+

表格票证

+----------+-----------+----------+
| TicketID | problemid | moduleid |
+----------+-----------+----------+
|     1233 | p1        | m1       |
|     1234 | p3        | m2       |
|     1235 | p1        | m1       |
|     1236 | p4        | m2       |
|     1237 | p8        | m4       |
+----------+-----------+----------+

我想要这样的输出:

+------------+-------------+----------------+
| modulename | problemname | num of tickets |
+------------+-------------+----------------+
| m1         | p1          |              2 |
| m1         | p2          |              0 |
| m2         | p3          |              1 |
| m2         | p4          |              1 |
| m3         | p5          |              0 |
| m3         | p6          |              0 |
| m4         | p7          |              0 |
| m4         | p8          |              1 |
+------------+-------------+----------------+

请帮助我,并提前致谢。

3 个答案:

答案 0 :(得分:1)

在这里列出一些列名:

select m.Module_name, p.Problem_name, count(t.TicketID) as no_tickets
from modules m
  left join ticket t on m.Moduleid = t.Moduleid
  left join problem p on t.problemid = p.problemid
group by m.Module_name, p.Problem_name

LEFT JOIN也可以毫无问题地返回模块。

答案 1 :(得分:1)

使用PDFLEFT JOIN

<强>查询

GROUP BY

答案 2 :(得分:0)

你有糟糕的表结构

更改您的表格结构,如

表格模块:

+----------+-------------+
|Moduleid(PK)| Module name |
+----------+-------------+
|        1 | m1          |
|        2 | m2          |
|        3 | m3          |
|        4 | m4          |
+----------+-------------+

表:问题

+-----------+----------+
| problemid(PK) | moduleid(FK) |
+-----------+----------+
| p1        | 1       |
| p2        | 1       |
| p3        | 2       |
| p4        | 2       |
| p5        | 3       |
| p6        | 3       |
| p7        | 4       |
| p8        | 4       |
+-----------+----------+

桌票:

+----------+-----------+----------+
| TicketID(PK) | problemid(FK) | moduleid(FK) |
+----------+-----------+----------+
|     1233 | p1        | 1       |
|     1234 | p3        | 2       |
|     1235 | p1        | 1       |
|     1236 | p4        | 2       |
|     1237 | p8        | 4       |
+----------+-----------+----------+
  

这个结构将帮助您在未来的发展或在   修改你的申请。

你可以尝试:

select m.ModuleName, p.ProblemName,count(TicketID ) from tbl_Module m
left join tbl_ticket t on m.ModuleId = t.ModuleId
  left join tbl_problem p on t.ProblemId = p.ProblemId
group by m.ModuleName, p.ProblemName