MySQL 查询,MAX() + GROUP BY + WHERE

标签 mysql max where-clause

如何获取包含每个分组集的最大值的行? 我有这个mysql

SELECT ticketId, version, storeId, `status` FROM ticket where storeId = 1 AND status = 0;

并得到结果。

enter image description here

我想在不同的ticketId中找到最大版本,行将是

ticket | version | storeId | status
---
  1    | 5       | 1       | 0  
  2    | 5       | 1       | 0

mysql 会是什么样子?

最佳答案

使用聚合函数max:

select ticketId,
    max(version),
    storeId,
    `status`
from ticket t
where storeId = 1
    and status = 0
group by ticketId,
    storeId,
    `status`

如果ONLY_FULL_GROUP_BY被禁用,您可以使用:

select ticketId,
    max(version),
    storeId,
    `status`
from ticket t
where storeId = 1
    and status = 0
group by ticketId;

关于MySQL 查询,MAX() + GROUP BY + WHERE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42240438/

相关文章:

mysql - 聊天数据库架构: is it correct to create a table for each chat room history?

php变量不存储

python - 使用python选择mysql中的零日期

excel - 如何在 SUMIFS 中包含两个总和范围(excel)

php - 在 MySQL 表中存储英文和中文字符的正确字符集是什么?

sql - 非数字列中的SQL MAX函数

python - 理论平均案例运行时复杂度为 `numpy.argmax()`

MySQL 更新过程包含多个 CASE 和 WHERE

php - 使用连接表中的值连接多个表

filter - Maximo ListView : Filter where field is null OR equals value?