【发布时间】:2016-06-19 14:26:02
【问题描述】:
我正在使用 MySQL。
我想要的结果是显示'res' = 'hans'的'time'最高的行,并将'frm'分组。
我正试图摆弄 GROUP BY、ORDER BY、MAX(time) - 我不会去哪里。
我的表:'消息'
| 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
我得到的“时间”是正确的,但“msg”和“opnd”是错误的。
我的代码:
SELECT frm, res, MAX(time), msg, opnd
FROM messages
WHERE res = 'hans'
GROUP BY frm
ORDER BY time DESC
【问题讨论】:
标签: mysql group-by max sql-order-by