如何获得最高价值的MySQL

问题描述 投票:0回答:1
SELECT DISTINCT
info_person.userUsername,
infoExamResult.resultPoint,
infoExamResult.resultPercentage,
infoExamResult.optAnswer
FROM info_person
INNER JOIN infoExamResult ON infoExamResult.personID = info_person.personID
where infoExamResult.datetimeStart BETWEEN "2024-03-18" AND "2024-03-26"
and infoExamResult.optAnswer = "COMMIT"
ORDER BY info_person.userUsername

结果

其中 resultPoint 是分数,resultPercentage 是最大分数的百分比

+-------+-----------+------------------+-------------------+-------------------+
|    userUsername   |    resultPoint   |  ResultPercentage |    optAnswer   |
+-------+-----------+------------------+-------------------+-------------------+
|    NSO001000      |      20          |       50%         |    COMMIT   |
|    NSO001000      |      10          |       25%         |    COMMIT   |
|    NSO002000      |      40          |       100%        |    COMMIT   |
|    NSO002000      |      30          |       75%         |    COMMIT   |
+-------------------+-------------------+------------------+-------------------+

我已经使用不同的用户名,但 1 个用户有 2 个结果点,我想获得最高点 我真的是 SQL 初学者

这就是我想要的样子

我在寻找什么

+-------+-----------+------------------+-------------------+-------------------+
|    userUsername   |    resultPoint   |  ResultPercentage |    optAnswer   |
+-------+-----------+------------------+-------------------+-------------------+
|    NSO001000      |      20          |       50%         |    COMMIT   |
|    NSO002000      |      40          |       100%        |    COMMIT   |
+-------------------+-------------------+------------------+-------------------+

我想获得最高结果点,但 Max 函数无法获得

sql mysql select
1个回答
0
投票

如果您只期望单个记录具有最高结果点,则使用

LIMIT
查询:

SELECT
    ip.userUsername,
    ip.resultPoint,
    ip.resultPercentage,
    ip.optAnswer
FROM info_person ip
INNER JOIN infoExamResult ier
    ON ier.personID = ip.personID
WHERE
    ier.datetimeStart BETWEEN '2024-03-18' AND '2024-03-26' AND
    ier.optAnswer = 'COMMIT'
ORDER BY
    ip.resultPoint DESC
LIMIT 1;
© www.soinside.com 2019 - 2024. All rights reserved.