简体   繁体   English

MySQL多表选择语句

[英]mysql select statement for multiple table

在此处输入图片说明

This is just a test project. 这只是一个测试项目。 I want to know how to select All professors with more than 5 failed students in a subject 我想知道如何选择一个学科中有5个以上失败学生的所有教授

I already know how to select All professors with at least 2 subjects with the following query: 我已经知道如何通过以下查询选择所有学科至少为2个的所有教授

SELECT    paulin_professors.*,
          IFNULL(sub_p.total, 0) num
FROM      paulin_professors
LEFT JOIN ( SELECT   COUNT(*) total, pau_profid
            FROM     paulin_profsubject
            GROUP BY pau_profid
          ) sub_p ON (sub_p.pau_profid = paulin_professors.pau_profid)
    WHERE sub_p.total >= 2;

I know I'm close but I can't get it to work (All professors with more than 5 failed students in a subject) . 我知道我已经接近了,但我无法使它起作用(所有教授在一个学科中有5个以上失败学生的教授)。 Any ideas? 有任何想法吗? TIA TIA

尝试将SELECT与UNION一起使用

select [columnName1],[columnName2] from [Table1] where [condition] union select [columnName1],[columnName2] from [Table1] where [condition] union ....

Looks like can get the professor IDs from the profsubject table and JOIN the studentenrolled table using the subjid for the join. 看起来可以从profsubject表中获取教授ID,并使用该subjid JOIN studentenrolled profsubject表。 In a similar way to what you had, you can get the count of students who have a grade less than a certain pass/fail threshold (in this case 65). 以与您所拥有的类似的方式,您可以获得成绩低于某个通过/未通过阈值(在本例中为65)的学生人数。

Then to get a short list, you can select the distinct profids from this derivaed table. 然后,要获取简短列表,可以从此派生表中选择不同的商品。

SELECT
    distinct pau_profid
FROM
    (SELECT    
        t1.pau_profid,
        IFNULL(t2.total_failed, 0) number_failed >= 5
    FROM      
        paulin_profsubject t1
    LEFT JOIN 
        (SELECT   
            COUNT(*) total_failed, 
            pau_subjid
        FROM     
            paulin_studentenrolled
        WHERE
            pau_grade < 65
        GROUP BY 
            pau_subjid
        ) t2
    ON 
        t1.pau_subjid = t2.pau_subjid
    WHERE 
        number_failed >= 5
    ) t3;

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

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