简体   繁体   English

如何从 MySQL 数据库中获取最大值

[英]How to get max value from a MySQL Database

I have an MYSQL database table of student info and their test scores per subject and I am trying to fetch each student's highest score in all subjects using the SQL query below我有一个 MYSQL 学生信息数据库表和他们每个科目的考试成绩,我正在尝试使用下面的 SQL 查询获取每个学生在所有科目中的最高分

SELECT DISTINCT
    first_name,
    last_name,
    subject_id,
    (SELECT 
            MAX(score)
        FROM
            cbt_attempts_tbl
        WHERE
            first_name = first_name) AS MAX_SCORE
FROM
    cbt_attempts_tbl
WHERE
    score IS NOT NULL
ORDER BY first_name DESC

sample data 样本数据

After running the query, the result I get are not as expected运行查询后,我得到的结果与预期不符

In your subselect you need to link both tables, for example by using an alias在您的子选择中,您需要链接两个表,例如通过使用别名

SELECT DISTINCT
    first_name,
    last_name,
    subject_id,
    (SELECT 
            MAX(score)
        FROM
            cbt_attempts_tbl
        WHERE
            first_name = f1.first_name) AS MAX_SCORE
FROM
    cbt_attempts_tbl f1
WHERE
    score IS NOT NULL
ORDER BY first_name DESC

But I don't really know what you are searching for, but following give the same, as DISTINCT doesn't make sense as long you don't have multiple rows with the same subject_id and name combination但我真的不知道你在搜索什么,但下面给出了相同的,因为只要你没有多行具有相同的 subject_id 和名称组合,DISTINCT 就没有意义

SELECT 
    first_name,
    last_name,
    subject_id,
    (SELECT 
            MAX(score)
        FROM
            cbt_attempts_tbl
        WHERE
            first_name = f1.first_name) AS MAX_SCORE
FROM
    cbt_attempts_tbl f1
WHERE
    score IS NOT NULL
GROUP BY first_name,
    last_name,
    subject_id
ORDER BY first_name DESC

If you are not worried about students without any score, then your query would be like如果您不担心没有任何分数的学生,那么您的查询就像

SELECT first_name, last_name, subject_id, MAX(score) FROM cbt_attempts_tbl GROUP BY first_name, last_name, subject_id ORDER BY first_name DESC

Otherwise, if you want to keep a full student list, you need to make a left joined query like否则,如果你想保留一个完整的学生列表,你需要做一个左连接查询,比如

SELECT DISTINCT t.first_name, t.last_name, scores.subject_id, scores.subject_max_score FROM cbt_attempts_tbl t LEFT JOIN (SELECT first_name, last_name, subject_id, MAX(score) AS subject_max_score FROM cbt_attempts_tbl GROUP BY first_name, last_name, subject_id) scores ON scores.first_name = t.first_name AND scores.last_name = t.last_name GROUP BY t.first_name, t.last_name, scores.subject_id ORDER BY t.first_name DESC

At last, if to be more comprehensive, your database design is not correct.最后,如果要更全面,你的数据库设计不正确。 You should have a students table and your cbt_attempts_tbl table should have a foreign key student_id that references a students table id column.你应该有一个students表,你的cbt_attempts_tbl表应该有一个引用studentsid列的外键student_id And then it would be completely different query - select from students left joined by cbt_attempts_tbl on foreign key condition然后它将是完全不同的查询 - select 来自在外键条件下由 cbt_attempts_tbl 加入的学生

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

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