简体   繁体   English

SQL / MySQL查询协助

[英]SQL/MySQL Query Assistance

I need some help with this SQL Query. 我需要有关此SQL查询的帮助。 It is designed to retrieve names of students with the same S.S_level values as Jaci Walker, and have taken courses (CS.C_SE_id) with Jaci Walker in the BG building. 它旨在检索具有与Jaci Walker相同的S.S_level值的学生的姓名,并已与Jaci Walker一起在BG建筑物中上过课程(CS.C_SE_id)。

I am having trouble on line 7. I need to be able to ensure that the people have enrolled in the same course as Jaci Walker. 我在第7行遇到了麻烦。我需要确保人们与Jaci Walker参加的课程相同。 I'm not sure about what to put in the WHERE statement for that section. 我不确定该部分的WHERE语句中应包含什么内容。

The database schema can be seen here: 数据库模式可以在这里看到: 数据库模式

SELECT S.S_Fname, S.S_LName
FROM Student S, Enrollment E, CourseSection CS, Location L
WHERE S.S_id = E.S_id
AND E.C_SE_ID = CS.C_SE_id
AND L.Loc_id = CS.Loc_ID
AND S.S_Level = (SELECT S.S_Level FROM Student S WHERE S.S_Fname = "Jaci" AND S.S_Lname = "Walker")
AND CS.C_SE_id = (SELECT CS.C_SE_id FROM CourseSection CS WHERE **?**)
AND L.Loc_id = (SELECT L.Blodg_code FROM Location L WHERE L.Blodg_code = "BG");

IF I'm understanding the question right this should work: 如果我了解正确的问题,则该方法应该起作用:

SELECT s.S_Fname, s.S_LName
FROM Student s

INNER JOIN Enrollment e ON s.S_id = e.S_id
INNER JOIN CourseSection cs ON e.C_SE_ID = cs.C_SE_id
INNER JOIN Location l ON cs.Loc_ID = l.Loc_id

INNER JOIN Student s2 ON s.S_Level = s2.S_Level AND s2.S_Fname = "Jaci" AND s2.S_Lname = "Walker"
INNER JOIN Enrollment e2 ON s2.S_id = e2.S_id
INNER JOIN CourseSection cs2 ON e2.C_SE_ID = cs2.C_SE_id

WHERE l.Loc_id = l.Blodg_code = "BG"
AND cs.Course_ID = cs2.Course_ID 

I'm unable to test at the moment and it was quickly done. 我目前无法测试,很快就完成了。 Maybe a better solution can be found? 也许可以找到更好的解决方案?

Without access to the data I can't test but the following may help (the first inner select is pulling back all the relevant C_SE_ID's) 在无法访问数据的情况下,我无法测试,但是以下操作可能会有所帮助(第一个内部选择是拉回所有相关的C_SE_ID)

SELECT S.S_Fname, S.S_Lname
FROM Student S, Enrollment E
WHERE S.S_id = E.S_id
        AND E.C_SE_ID IN (SELECT CS.C_SE_ID
        FROM Student S2, Enrollment E2, CourseSection CS, Location L
        WHERE S2.S_id = E2.S_id
                AND E2.C_SE_ID = CS.C_SE_ID
                AND CS.Loc_id = L.Loc_id
                AND L.Blodg_code = 'BG'
                AND S2.S_Fname = 'Jaci'
                AND S2.S_Lname = 'Walker')
        AND S.S_Level = (SELECT S3.S_Level
        FROM Student S3
        WHERE S3.S_Fname = 'Jaci'
                AND S3.S_Lname = 'Walker')
        AND S.S_Fname <> 'Jaci'
        AND S.S_Lname <> 'Walker'

I would recommend using different aliases within inner queries (eg S2, E2) to avoid confusion. 我建议在内部查询(例如S2,E2)中使用不同的别名,以避免混淆。

I would start by using current SQL-syntax using JOIN conditions instead of using the WHERE clause to show relationships between tables. 我将从使用当前的SQL语法和JOIN条件开始,而不是使用WHERE子句显示表之间的关系。 This way, you get all your table associations done and can better visually confirm you have those elements configured... THEN, tack on the criteria you are looking for. 这样,您就可以完成所有表的关联,并且可以更好地从视觉上确认您已配置了那些元素...然后,根据要查找的条件进行操作。

What I have done here is to just have a PreQuery (result alias "JaciClassesInBG" ) that gets all of Jaci's classes that were enrolled in and ONLY those for the building "BG" (which was added to the JOIN clause to the location table). 我在这里所做的只是拥有一个PreQuery(结果别名为“ JaciClassesInBG”),该查询获取所有已注册的Jaci类,并且仅包含用于构建“ BG”的类(已将其添加到位置表的JOIN子句中) 。 The WHERE clause was only for Jaci. WHERE子句仅适用于Jaci。

From that result, I have a list of all classes that Jaci took. 根据该结果,我列出了Jaci参加的所有课程。 I grabbed her ID, S_Level and C_SE_ID entries. 我抓住了她的ID,S_Level和C_SE_ID条目。

From that, just join back to the enrollment table of all other students based explicitly on the C_SE_ID that Jaci took (thus all students in that exact same class). 然后,仅根据Jaci所使用的C_SE_ID(因此完全属于同一班的所有学生),直接返回所有其他学生的注册表。 However, I've EXCLUDED (via AND NOT...) Jaci's student ID from the list... we know she took the class, we are looking for everyone ELSE. 但是,我已经从列表中排除了(通过AND NOT ...)Jaci的学生证...我们知道她参加了这堂课,我们正在寻找所有人。

Finally, join that result back to the students table based on the common enrollment. 最后,根据普通入学将结果返回学生表。 Now, we can associate the common "S_LEVEL" criteria of Jaci to those students... 现在,我们可以将Jaci的常见“ S_LEVEL”标准与这些学生相关联...

Now, you can get whatever details you want for display... in this case, I am grabbing each student, and what class they had in common with Jaci. 现在,您可以获得想要显示的任何详细信息...在这种情况下,我要抓住每个学生,以及他们与Jaci有何共同点。 One student may have been in multiple classes. 一名学生可能参加过多个课程。 This will show each. 这将显示每个。 If you only care about one instance, I would just change the top to... 如果您只关心一个实例,我只需将顶部更改为...

select DISTINCT S2.S_FName, S2.S_LName... 选择DISTINCT S2.S_FName,S2.S_LName ...

SELECT
      JaciClassesInBG.Course_Code,
      JaciClassesInBG.Course_Name,
      S2.S_FName,
      S2.S_LName
   from 
      ( SELECT 
              S.ID,
              S.S_Level,
              CS.C_SE_ID,
              C.Course_Code,
              C.Course_Name
           FROM
              Student S
                 JOIN Enrollment E
                    ON S.S_id = E.S_id
                    JOIN CourseSection CS
                       ON E.C_SE_ID = CS.C_SE_id
                       JOIN Location L
                          ON L.Loc_id = CS.Loc_ID
                          AND L.Blodg_Code = "BG"
                    JOIN Course C
                       ON CS.Course_ID = C.Course_ID
           WHERE
                  S.S_Fname = "Jaci" 
              AND S.S_Lname = "Walker" ) JaciClassesInBG
      JOIN
         Enrollment E2
            ON JaciClassesInBG.C_SE_ID = E2.C_SE_ID
            AND NOT JaciClassesInBG.S_ID = E2.S_ID
            JOIN Students S2
               ON E2.S_ID = S2.S_ID
               AND JaciClassesInBG.S_Level = S2.S_Level

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM