php mysql group by获取最新记录,而不是第一条记录

pvabu6sv  于 2021-06-18  发布在  Mysql
关注(0)|答案(5)|浏览(419)

表格:

  1. (`post_id`, `forum_id`, `topic_id`, `post_time`)
  2. (79, 8, 4, '2012-11-19 06:58:08');
  3. (80, 3, 3, '2012-11-19 06:58:42'),
  4. (81, 9, 9, '2012-11-19 06:59:04'),
  5. (82, 11, 6, '2012-11-19 16:05:39'),
  6. (83, 9, 9, '2012-11-19 16:07:46'),
  7. (84, 9, 11, '2012-11-19 16:09:33'),

查询:

  1. SELECT post_id, forum_id, topic_id FROM posts
  2. GROUP BY topic_id
  3. ORDER BY post_time DESC
  4. LIMIT 5

结果是:

  1. [0] => [post_id] => 84 [forum_id] => 9 [topic_id] => 11
  2. [1] => [post_id] => 82 [forum_id] => 11 [topic_id] => 6
  3. [2] => [post_id] => 81 [forum_id] => 9 [topic_id] => 9
  4. [3] => [post_id] => 80 [forum_id] => 3 [topic_id] => 3
  5. [4] => [post_id] => 79 [forum_id] => 8 [topic_id] => 4

问题是:
如何重写查询,使其返回post\u id->83而不是post\u id->81?
它们都有相同的论坛和主题id,但是post\u id->81的日期比post\u id->83的日期早。
但似乎groupby得到的是“第一”张唱片,而不是“最新”的。
我试着把查询改成

  1. SELECT post_id, forum_id, topic_id, MAX(post_time)

但它同时返回81号和83号邮政编码

hmtdttj4

hmtdttj41#

这对你也很有用。

  1. SELECT *
  2. FROM (
  3. SELECT post_id, forum_id, topic_id FROM posts
  4. ORDER BY post_time DESC
  5. LIMIT 5
  6. ) customeTable
  7. GROUP BY topic_id
uinbv5nw

uinbv5nw2#

试试这样的

  1. SELECT post_id, forum_id, topic_id
  2. FROM (SELECT post_id, forum_id, topic_id
  3. FROM posts
  4. ORDER BY post_time DESC)
  5. GROUP BY topic_id
  6. ORDER BY topic_id desc
  7. LIMIT 0,5

更改 order by 以及 limit 根据需要。

zu0ti5jz

zu0ti5jz3#

它不是很漂亮,但很管用:

  1. SELECT * FROM (SELECT post_id, forum_id, topic_id FROM posts
  2. ORDER BY post_time DESC) as temp
  3. GROUP BY topic_id
qzlgjiam

qzlgjiam4#

也许这不是最好的方法,但有时函数组\u concat()可以是userfull,它将返回一个字符串,其中包含所有聚合值,按需要排序,并用逗号分隔(耦合值用空格分隔)。然后我使用函数split\u string()来剪切字符串中的第一个id。

  1. SELECT
  2. post_id,
  3. SPLIT_STRING( group_concat( forum_id, post_time ORDER BY post_time DESC ) ,' ',1 )as forum_id,
  4. SPLIT_STRING( group_concat( topic_id, post_time ORDER BY post_time DESC ) ,' ',1 )as topic_id ,
  5. FROM posts
  6. GROUP BY topic_id
  7. ORDER BY post_time DESC
  8. LIMIT 5

所以聚合的论坛id,发布时间如下:
81 2012-11-19 06:59:04,83 2012-11-19 16:07:46
因此,您需要使用整数和日期时间对的字符串表示,每个对用逗号分隔,因此我使用此函数来获取第一个int:

  1. CREATE FUNCTION SPLIT_STRING(str VARCHAR(255), delim VARCHAR(12), pos INT)
  2. RETURNS VARCHAR(255)
  3. RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(str, delim, pos),
  4. LENGTH(SUBSTRING_INDEX(str, delim, pos-1)) + 1),
  5. delim, '');

注意:函数split\u string(str,delim,pos)在这里找到:相当于explode()在mysql中处理字符串

展开查看全部
lvmkulzt

lvmkulzt5#

如果选择group子句中未使用且不是聚合的属性,则结果未指定。i、 e您不知道从哪些行中选择其他属性(sql标准不允许这样的查询,但mysql更为宽松)。
然后应将查询写入,例如

  1. SELECT post_id, forum_id, topic_id
  2. FROM posts p
  3. WHERE post_time =
  4. (SELECT max(post_time) FROM posts p2
  5. WHERE p2.topic_id = p.topic_id
  6. AND p2.forum_id = p.forum_id)
  7. GROUP BY forum_id, topic_id, post_id
  8. ORDER BY post_time DESC
  9. LIMIT 5;

或者

  1. SELECT post_id, forum_id, topic_id FROM posts
  2. NATURAL JOIN
  3. (SELECT forum_id, topic_id, max(post_time) AS post_time
  4. FROM posts
  5. GROUP BY forum_id, topic_id) p
  6. ORDER BY post_time
  7. LIMIT 5;
展开查看全部

相关问题