我有3张桌子:
分部(ID,分部)
团队(ID,团队)
分数(ID,RelationID,分数)
亲属(ID,RelationID,FieldID,ValID)
亲属表是为了链接' RelationID的部门和团队
FieldID定义了表格(1 =分部; 2 =团队)
ValID =给定表的ID
所以,我的fk是FieldID& amp;有效
输出应该如下所示:
Southwest Division | Tomcats | 17 Southwest Division | Bears | 12 Northeast Division | Tomcats | 20 Northeast Division | Lions | 8 Northeast Division | Cheetahs | 13 Southeast Division | Cheetahs | 19 Southeast Division | Lions | 12 Southeast Division | Zebras | 6
任何特定的团队都可以在任何部门。
我的Sql是:
SELECT DISTINCT divisions.Division, teams.Team, AVG(scores.Score) AS Score FROM
(answers INNER JOIN
((relatives
INNER JOIN divisions ON relatives.FieldID = 1 AND relatives.ValID = divisions.ID)
INNER JOIN teams ON relatives.FieldID = 2 AND relatives.ValID = teams.ID)
ON answers.RelationID = relatives.RelationID)
GROUP BY Division, Team
ORDER BY Division, Team
此sql不返回任何记录。即使我删除了答案,GROUP和ORDER BY也没有记录。
如何让表格返回我需要的? 如果需要,我会重新设计整个事物。