我有三个表。使用LEFT JOIN在查询中添加列
tblcandidates
candidateid | candidatename
1 | Abc
2 | Def
tbljudges
judgeid | judgename
1 | Stack
2 | Overflow
tblscores
scoreid | candidateid | judgeid | swimsuit
1 | 1 | 1 | 100
2 | 1 | 2 | 99
3 | 2 | 1 | 100
4 | 2 | 2 | 93
我使用此查询得到每个候选人的平均数。
SELECT DISTINCT
(c.candidateid) AS c,
candidatename AS NAME,
j1.swimsuit AS j1,
j2.swimsuit AS j2,
(
j1.swimsuit + j2.swimsuit
)/2 AS average
FROM
tblscores,
tblcandidates c
LEFT JOIN tblscores j1 ON c.candidateid = j1.candidateid
AND j1.judgeid = 1
LEFT JOIN tblscores j2 ON c.candidateid = j2.candidateid
AND j2.judgeid = 2
WHERE tblscores.candidateid = c.candidateid;
输出
c | name | j1 | j2 | average
1 | Abc | 100 | 99 | 99.5
2 | Def | 100 | 93 | 96.5
我的问题是什么,如果法官成为3.我要让我的查询动视法官的人数。我的查询仅限2名评委。我还想显示评分得分,就像我输出的结果一样,以证明他们有得分。
请分享一个sql小提琴。这是一个MySQL数据透视表问题。 – 1000111
如何制作一个sql小提琴? –
下面是一个示例[** sql fiddle **](http://sqlfiddle.com/#!9/3fd52/1/0) – 1000111