mysql - 在子查询中选择最大值

标签 mysql sql subquery max greatest-n-per-group

我有这两个表:

学生:

|                name |                   email |
|---------------------|-------------------------|
|    Arturo     Vidal |     <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97f6e5e3e2e5f8b9e1fef3f6fbd7e2e4fab9f4fb" rel="noreferrer noopener nofollow">[email protected]</a> |
|   Bastian   Quezada |          <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b090a181f020a052b1e1806450807" rel="noreferrer noopener nofollow">[email protected]</a> |
|    Javier     Jeria |           <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92f8f3e4fbf7e0d2e7e1ffbcf1fe" rel="noreferrer noopener nofollow">[email protected]</a> |
| Sebastian    Piñera | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c3f292e2d3f38252d220c3c3e293f252829223829622f20" rel="noreferrer noopener nofollow">[email protected]</a> |
| Sebastian  Gallardo |        <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2d1c7c0c3d1d6cbc3cce2d7d1cf8cc1ce" rel="noreferrer noopener nofollow">[email protected]</a> |

类(class):

| classId |                   email |  signUpDate |
|---------|-------------------------|-------------|
|       1 |        <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a594f484b595e434b446a5f5947044946" rel="noreferrer noopener nofollow">[email protected]</a> |  2018-01-01 |
|       1 |           <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3852594e515d4a784d4b55165b54" rel="noreferrer noopener nofollow">[email protected]</a> |  2019-10-01 |
|       1 |          <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f5d5e4c4b565e517f4a4c52115c53" rel="noreferrer noopener nofollow">[email protected]</a> |  2018-07-01 |
|       2 |        <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddaeb8bfbcaea9b4bcb39da8aeb0f3beb1" rel="noreferrer noopener nofollow">[email protected]</a> |  2018-05-04 |
|       2 |          <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aecccfdddac7cfc0eedbddc380cdc2" rel="noreferrer noopener nofollow">[email protected]</a> |  2018-01-01 |
|       3 |          <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee8c8f9d9a878f80ae9b9d83c08d82" rel="noreferrer noopener nofollow">[email protected]</a> |  2018-12-05 |
|       3 |        <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcafb9bebdafa8b5bdb29ca9afb1f2bfb0" rel="noreferrer noopener nofollow">[email protected]</a> |  2018-02-01 |
|       4 |     <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1071626465627f3e667974717c5065637d3e737c" rel="noreferrer noopener nofollow">[email protected]</a> |  2018-03-01 |
|       5 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0b3a5a2a1b3b4a9a1ae80b0b2a5b3a9a4a5aeb4a5eea3ac" rel="noreferrer noopener nofollow">[email protected]</a> |  2018-03-01 |

我想显示最后注册每个 classId 的学生的姓名。这意味着,我应该为 classId 1 获取一个名称,为 classId 2 获取一个名称,等等。我首先获取邮件(之后知道学生姓名)的解决方案是这样的:

select classId, email, max(signUpDate)
from Class
group by classId

它打印最大日期,这是可以的,但它也会为每个日期打印错误的邮件:

| ClassId |                   email | max(signUpDate) |
|---------|-------------------------|-----------------|
|       1 |        <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5d6c0c7c4d6d1ccc4cbe5d0d6c88bc6c9" rel="noreferrer noopener nofollow">[email protected]</a> |      2019-10-01 |
|       2 |        <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3b0a6a1a2b0b7aaa2ad83b6b0aeeda0af" rel="noreferrer noopener nofollow">[email protected]</a> |      2018-05-04 |
|       3 |          <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b494a585f424a456b5e5846054847" rel="noreferrer noopener nofollow">[email protected]</a> |      2018-12-05 |
|       4 |     <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfdecdcbcacdd091c9d6dbded3ffcaccd291dcd3" rel="noreferrer noopener nofollow">[email protected]</a> |      2018-03-01 |
|       5 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e29187808391968b838ca2929087918b86878c9687cc818e" rel="noreferrer noopener nofollow">[email protected]</a> |      2018-03-01 |

这是完全错误的()。因此,当我尝试加入值来获取名称时,我得到的值不正确。

换句话说,我不明白为什么行会混淆。是否有任何解决方案可以为每个 ClassId 的 max(signUpDate) 获取正确的电子邮件?

感谢您的宝贵时间

最佳答案

我已经创建了测试数据 fiddle ,并进行了一个简单易懂的查询来获取所需的数据,即:

SELECT DISTINCT classId,
                std.name,
                Class.email,
                signUpDate
FROM CLASS
INNER JOIN Student std ON std.email = Class.email
WHERE signUpDate IN
    (SELECT max(signUpDate)
     FROM CLASS
     GROUP BY classId)

Sql Fiddle here

关于mysql - 在子查询中选择最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49534233/

相关文章:

php - MySQL,如何获取后缀最高的字符串

php - 更新存储在数据库中的 Json 字符串

sql - 在 SQL 2008 中恢复多个数据库的最有效方法是什么

mysql - 外键识别关系和非识别关系之间的性能差异

sql - 多列索引的顺序和SQL查询

mysql - SQL插入多个记录/更新现有(在单个查询中)

mysql - 如何检查一致性约束,该约束显示用户不属于他们应该属于的属性?

mysql - 单个查询以获取消息并计算他们的答案

MySQL WHERE EXISTS 不工作

postgresql - 在 SQL 中编写子查询