在MySql中,你可以使用MAX()
函数来获取最高值GROUP BY
,我怎样才能做同样的事情来获取最长的文本字符串?
样品表:
id_|_post_id|_title__________|_body_____________________________________________
1 | ZXBF1J | Favorite Color | My favorite color is blue.
2 | ZXBF1J | Favorite Color | My favorite color is blue, no wait...
3 | ZXBF1J | Favorite Color | My favorite color is blue, no wait, yelloooow!
4 | AR3D47 | Quest | To seek..
5 | AR3D47 | Quest | To seek the Holy
6 | AR3D47 | Quest | To seek the Holy Grail.
棘手的部分是我想ORDER BY id ASC
在顶部查看最旧的条目,并且我想按post_id
我无法使用的内容进行分组ORDER
,并获得最长的条目body
。
示例查询:
SELECT post_id, title, MAX(body) // obviously MAX() doesn't work here
FROM posts
GROUP BY post_id
ORDER BY id ASC
期望的输出:
post_id|_title__________|_body_____________________________________________
ZXBF1J | Favorite Color | My favorite color is blue, no wait, yelloooow!
AR3D47 | Quest | To seek the Holy Grail.
同样,关键是选择最长的body
同时保持基于id
.