按 max() 顺序分组
group by order by max()
我正在使用 MySQL。
我想要的结果是显示 'time' 最高的行,其中 'res' = 'hans',并将 'frm'.
分组
我正在尝试 fiddle 使用 GROUP BY、ORDER BY、MAX(time) - 我哪儿也去不了。
我的table:'messages'
| frm | res | time | msg | opnd |
| poul | hans | 0916 | hi there | 1 |
| john | hans | 1033 | waz up | 1 |
| hans | john | 1140 | new text | 0 |
| poul | john | 1219 | message | 0 |
| poul | hans | 1405 | respond | 0 |
| john | hans | 1544 | write | 0 |
我想要的结果:
poul - hans - 1405 - respond - 0
john - hans - 1544 - write - 0
我得到的结果:
poul - hans - 1405 - hi there - 1
john - hans - 1544 - waz up - 1
我得到正确的 'time' 但错误的 'msg' 和 'opnd'.
我的代码:
SELECT frm, res, MAX(time), msg, opnd
FROM messages
WHERE res = 'hans'
GROUP BY frm
ORDER BY time DESC
有几种方法可以做到这一点。一种是使用子查询 join
回到原来的 table:
SELECT m.*
FROM messages m
JOIN (
SELECT frm, res, MAX(time) maxtime
FROM messages
WHERE res = 'hans'
GROUP BY frm, res) m2 on m.frm = m2.frm
and m.res = m2.res
and m.time = m2.maxtime
ORDER BY m.time DESC
Mysql
允许您从 group by
子句中省略未在聚合中使用的字段(imo 错误 - 大多数其他数据库不允许此行为)。通过允许它,它只是 returns 一个随机结果,尽管这就是您所经历的。
这是另一种使用 outer join
的方法,但我认为前一种方法更容易理解:
select m.*
from messages m
left join messages m2 on m.frm = m2.frm
and m.res = m2.res
and m2.time > m.time
where m2.frm is null
and m.res = 'hans'
order by m.time desc
你的问题是你按一列分组,但你select用它分组了几列。结果,对于其他未按列分组的结果,您只会得到其中一个结果,不一定是属于 max(time) 值的结果。
你需要这样的东西:
select a.frm, a.res, b.max_time, a.msg, a.opnd from
messages as a inner join
(SELECT frm, MAX(time) as max_time
FROM messages
WHERE res = 'hans'
GROUP BY frm) on a.frm = b.frm and a.time = b.max_time
ORDER BY time DESC
我正在使用 MySQL。
我想要的结果是显示 'time' 最高的行,其中 'res' = 'hans',并将 'frm'.
分组我正在尝试 fiddle 使用 GROUP BY、ORDER BY、MAX(time) - 我哪儿也去不了。
我的table:'messages'
| frm | res | time | msg | opnd |
| poul | hans | 0916 | hi there | 1 |
| john | hans | 1033 | waz up | 1 |
| hans | john | 1140 | new text | 0 |
| poul | john | 1219 | message | 0 |
| poul | hans | 1405 | respond | 0 |
| john | hans | 1544 | write | 0 |
我想要的结果:
poul - hans - 1405 - respond - 0
john - hans - 1544 - write - 0
我得到的结果:
poul - hans - 1405 - hi there - 1
john - hans - 1544 - waz up - 1
我得到正确的 'time' 但错误的 'msg' 和 'opnd'.
我的代码:
SELECT frm, res, MAX(time), msg, opnd
FROM messages
WHERE res = 'hans'
GROUP BY frm
ORDER BY time DESC
有几种方法可以做到这一点。一种是使用子查询 join
回到原来的 table:
SELECT m.*
FROM messages m
JOIN (
SELECT frm, res, MAX(time) maxtime
FROM messages
WHERE res = 'hans'
GROUP BY frm, res) m2 on m.frm = m2.frm
and m.res = m2.res
and m.time = m2.maxtime
ORDER BY m.time DESC
Mysql
允许您从 group by
子句中省略未在聚合中使用的字段(imo 错误 - 大多数其他数据库不允许此行为)。通过允许它,它只是 returns 一个随机结果,尽管这就是您所经历的。
这是另一种使用 outer join
的方法,但我认为前一种方法更容易理解:
select m.*
from messages m
left join messages m2 on m.frm = m2.frm
and m.res = m2.res
and m2.time > m.time
where m2.frm is null
and m.res = 'hans'
order by m.time desc
你的问题是你按一列分组,但你select用它分组了几列。结果,对于其他未按列分组的结果,您只会得到其中一个结果,不一定是属于 max(time) 值的结果。
你需要这样的东西:
select a.frm, a.res, b.max_time, a.msg, a.opnd from
messages as a inner join
(SELECT frm, MAX(time) as max_time
FROM messages
WHERE res = 'hans'
GROUP BY frm) on a.frm = b.frm and a.time = b.max_time
ORDER BY time DESC