【发布时间】:2014-04-24 00:01:18
【问题描述】:
我有 3 张桌子:
人:
pid name
1 Cal
2 Example
3 Another
4 Person
谈话要点:
tid pid talkingPoint
1 1 "..."
2 1 "..."
3 2 "..."
事实:
fid pid fact
1 3 "..."
2 2 "..."
我正在尝试将 talkPoints 和事实的计数与“人”结合起来,例如:
pid name talkingPoints facts
1 Cal 2 null
2 Example 1 1
3 Another null 1
4 Person null null
(按talkPoints desc 排序,然后按字母顺序,包括没有任何计数值的“人”行)
我设法将“人”与另一张桌子结合起来:
SELECT a.pid,a.name,
count(b.tid)
FROM people a, talkingPoints b
WHERE a.pid=b.pid
GROUP BY b.pid;
但该查询会忽略计数为零的行(例如“Person”行)
我破解了这个对“talkingPoints”正常工作的查询,但我无法像上面的示例表一样对其进行调整以结合“事实”。
select people.pid, people.name, x.talkingPoints from people left join
(select pid, name, count(name) talkingPoints from
(select people.pid, people.name from people
join talkingPoints on talkingPoints.pid = people.pid)
as talkingPoints group by talkingPoints.pid)
as x on x.pid = people.pid order by talkingPoints desc, people.name asc;
(可能是一种糟糕的方式,但同时也奏效了)
我如何调整我的查询,以便它们输出一个像我的示例一样的表?
【问题讨论】: