0

我正在处理 SQL 查询。我有以下表格

  • Class( ID,Name)
  • Competition( ID,Name)
  • Month( ID,Name)
  • Student( ID,Name,Class,MonthEnrollment)

所有这些表都已在表中引用

Competition_Result(ID,Class,Competiton,Month,First,Second,Third)

First, Second 和是 students_ID。为了获得第 1 个月的比赛结果,我使用了以下 sql 查询:

SELECT     
    Competitions.Name AS Comp, Students.Name AS Student, 
    Class.Name AS ClassName, Competition_Result.*
FROM Competition_Result 
INNER JOINCompetitions ON Competition_Result.Competition = Competitions.ID 
INNER JOIN Students ON Competition_Result.First = Students.ID AND Competition_Result.Second = Students.ID AND  Competition_Result.Third = Students.ID 
INNER JOIN Class ON Competition_Result.Class = Class.ID

但它不起作用..请帮助我

4

1 回答 1

1

你必须多次加入表学生,因为你的第一,第二和第三名是不同的,所以如果三个值相同,没有什么可以满足条件(想想传递性),查询会起作用,但这似乎是在你的模型中是不可能的。

ON  Competition_Result.First = Students.ID 
AND Competition_Result.Second = Students.ID 
AND Competition_Result.Third = Students.ID

试试这个查询(虽然很难说你的问题是什么。)

SELECT 
    competitions.name as comp, 
    first.name as first, 
    second.name as second, 
    third.name as third, 
    class.name as classname, 
    competition_result.*
FROM competition_result 
    INNER JOIN competitions 
        ON competition_result.competition = competitions.id 
    INNER JOIN students first
        ON competition_result.first = first.id 
    INNER JOIN students second
        ON competition_result.second = second.id
    INNER JOIN students third
        ON competition_result.third = third.id 
    INNER JOIN class 
        ON competition_result.class = class.id
于 2013-09-14T14:05:54.913 回答