查询错误

发布于 2024-11-04 06:03:25 字数 1862 浏览 0 评论 0原文

SELECT Student.S_ID, Student.First_Name, Student.Surname, 
       MAX(New_Models.Date_Issued) AS Last_Course_Date, 
       MAX(New_Models.Issue) AS Last_Issue, 
       MAX(New_Models.Model_ID) AS Last_Model_ID,
       Student.Course_Level AS No_Training_Courses,
       **COUNT(Exam_Student.Exam_ID) AS Final_Exam_Level**
FROM New_Models, Model_Student, Student, Exams, Exam_Student
WHERE New_Models.Model_ID=Model_Student.Model_ID 
  AND Student.S_ID=Model_Student.S_ID
  AND (Student.S_ID)=Exam_Student.S_ID
  AND ((Exams.Exam_ID)=Exam_Student.Exam_ID)
  AND (Exams.Date_Taken)<=#12/31/2010#
GROUP BY Student.S_ID, First_Name, Surname, S.Course_Level
ORDER BY Student.S_ID, MAX(New_Models.Model_ID) DESC;

除 COUNT 命令外,所有结果均按要求显示。当与应该出现的实际结果进行比较时,只有第一个单元格不同并且是错误的。 应该计算学生完成了多少次考试。

我可以回答任何问题,等待您的帮助。

SELECT 
    Student.S_ID, 
    Student.first_name, 
    Student.surname, 
    MAX(new_models.date_issued) AS last_course_date, 
    MAX(new_models.issue) AS last_issue, 
    MAX(new_models.model_id) AS last_model_id, 
    Student.course_level AS no_training_courses 
FROM 
    new_models, 
    Student, 
    Model_Student 
WHERE 
    new_models.model_id = Model_Student.model_id 
    AND Student.S_id = Model_Student.S_id 
    AND *strong text*Exam_ID IN ( 
            SELECT COUNT(Exam_Student.Exam_id) AS Final_Exam_Level 
            FROM 
                Exams, 
                Exam_Student, 
                Student 
            WHERE 
                Student.S_ID = Exam_Student.S_ID 
                AND exams.date_taken <=#12/31/2010# 
            Group by Student.S_ID) 
GROUP BY 
    Student.S_ID,
    first_name, 
    surname, 
    Student.Course_level 
ORDER BY 
    Student.S_id, 
    MAX(new_models.model_id) DESC;

我不能使用 INNER JOIN 所以,我想知道可以完成类似上面的事情。但是,我不知道如何进行子查询!

SELECT Student.S_ID, Student.First_Name, Student.Surname, 
       MAX(New_Models.Date_Issued) AS Last_Course_Date, 
       MAX(New_Models.Issue) AS Last_Issue, 
       MAX(New_Models.Model_ID) AS Last_Model_ID,
       Student.Course_Level AS No_Training_Courses,
       **COUNT(Exam_Student.Exam_ID) AS Final_Exam_Level**
FROM New_Models, Model_Student, Student, Exams, Exam_Student
WHERE New_Models.Model_ID=Model_Student.Model_ID 
  AND Student.S_ID=Model_Student.S_ID
  AND (Student.S_ID)=Exam_Student.S_ID
  AND ((Exams.Exam_ID)=Exam_Student.Exam_ID)
  AND (Exams.Date_Taken)<=#12/31/2010#
GROUP BY Student.S_ID, First_Name, Surname, S.Course_Level
ORDER BY Student.S_ID, MAX(New_Models.Model_ID) DESC;

All the result appear as required except the COUNT command. When comparing with actual result whihc should come only the first cell differs and is wrong.
It should be counting how many exams has the student done.

I can answer any questions, will be waiting for your help.

SELECT 
    Student.S_ID, 
    Student.first_name, 
    Student.surname, 
    MAX(new_models.date_issued) AS last_course_date, 
    MAX(new_models.issue) AS last_issue, 
    MAX(new_models.model_id) AS last_model_id, 
    Student.course_level AS no_training_courses 
FROM 
    new_models, 
    Student, 
    Model_Student 
WHERE 
    new_models.model_id = Model_Student.model_id 
    AND Student.S_id = Model_Student.S_id 
    AND *strong text*Exam_ID IN ( 
            SELECT COUNT(Exam_Student.Exam_id) AS Final_Exam_Level 
            FROM 
                Exams, 
                Exam_Student, 
                Student 
            WHERE 
                Student.S_ID = Exam_Student.S_ID 
                AND exams.date_taken <=#12/31/2010# 
            Group by Student.S_ID) 
GROUP BY 
    Student.S_ID,
    first_name, 
    surname, 
    Student.Course_level 
ORDER BY 
    Student.S_id, 
    MAX(new_models.model_id) DESC;

I cannot use INNER JOIN so, I was wondering that something like the above can be done. However, I dont know how to do the subquery!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轮廓§ 2024-11-11 06:03:25

一种可能有帮助的方法是简单地在计数上创建一个子查询,然后连接到该子查询。

我还建议使用 ANSI-92 样式连接

SELECT student.s_id, 
       student.first_name, 
       student.surname, 
       MAX(new_models.date_issued) AS last_course_date, 
       MAX(new_models.issue)       AS last_issue, 
       MAX(new_models.model_id)    AS last_model_id, 
       student.course_level        AS no_training_courses,
       Examqry.ExamCount 
FROM   new_models 
       INNER JOIN model_student ON  new_models.model_id = model_student.model_id 
       INNER JOIN student ON student.s_id = model_student.s_id 
       INNER JOIN (SELECT COUNT(Exam_id) ExamCount , 
                                         s_id 
                           FROM exams Group by s_id
                          Where exams.date_taken <=#12/31/2010#) examqry
       ON student.s_id = examqry.s_id

GROUP  BY student.s_id, 
          first_name, 
          surname, 
          s.course_level 
ORDER  BY student.s_id, 
          MAX(new_models.model_id) DESC; 

One approach that may help is to simply create a subquery on the Counts and then join to that.

I also recommend using ANSI-92 style joins

SELECT student.s_id, 
       student.first_name, 
       student.surname, 
       MAX(new_models.date_issued) AS last_course_date, 
       MAX(new_models.issue)       AS last_issue, 
       MAX(new_models.model_id)    AS last_model_id, 
       student.course_level        AS no_training_courses,
       Examqry.ExamCount 
FROM   new_models 
       INNER JOIN model_student ON  new_models.model_id = model_student.model_id 
       INNER JOIN student ON student.s_id = model_student.s_id 
       INNER JOIN (SELECT COUNT(Exam_id) ExamCount , 
                                         s_id 
                           FROM exams Group by s_id
                          Where exams.date_taken <=#12/31/2010#) examqry
       ON student.s_id = examqry.s_id

GROUP  BY student.s_id, 
          first_name, 
          surname, 
          s.course_level 
ORDER  BY student.s_id, 
          MAX(new_models.model_id) DESC; 
对岸观火 2024-11-11 06:03:25

我猜测没有看到示例数据,但也许可以尝试这个,更改

AND (Student.S_ID)=Exam_Allocation.S_ID
AND ((Exams.Exam_ID)=Exam_Allocation.Exam_ID)

AND Student.S_ID=Exams.Exam_ID
AND Exams.Exam_ID=Exam_Allocation.Exam_ID

如果这没有帮助,您将需要发布一些示例数据以及预期和实际结果以供某人帮助。

I'm guessing without seeing example data but maybe try this, change

AND (Student.S_ID)=Exam_Allocation.S_ID
AND ((Exams.Exam_ID)=Exam_Allocation.Exam_ID)

to

AND Student.S_ID=Exams.Exam_ID
AND Exams.Exam_ID=Exam_Allocation.Exam_ID

If this does not help you will need to post some example data with expected and actual results for someone to help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文