MySQL 获取每个位置的最后结果

标签 mysql select where-clause greatest-n-per-group

<分区>

我有 3 个问题:

SELECT `update`.`id` AS `id`, `update`.`type` AS `type`, `update`.`date` AS `date`, `update`.`like` AS `like`, `update`.`dislike` AS `dislike` FROM `updates` AS `update` WHERE `type` = '1' ORDER BY `date` DESC LIMIT 1

SELECT `update`.`id` AS `id`, `update`.`type` AS `type`, `update`.`date` AS `date`, `update`.`like` AS `like`, `update`.`dislike` AS `dislike` FROM `updates` AS `update` WHERE `type` = '2' ORDER BY `date` DESC LIMIT 1 (1)

SELECT `update`.`id` AS `id`, `update`.`type` AS `type`, `update`.`date` AS `date`, `update`.`like` AS `like`, `update`.`dislike` AS `dislike` FROM `updates` AS `update` WHERE `type` = '3' ORDER BY `date` DESC LIMIT 1 (1)

我需要获取每种类型的最后日期。如何在一个查询中获取它?

谢谢。

最佳答案

这一定是 #1 最常见的 mysql 问题。

SELECT u.*
FROM update u
JOIN (SELECT type, MAX(date) maxdate
      FROM update
      WHERE type in ('1', '2', '3')
      GROUP BY type) m
ON u.type = m.type AND u.date = m.maxdate

关于MySQL 获取每个位置的最后结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17598889/

相关文章:

mysql - SQL SELECT 如何仅获取事件记录

mysql - SQL : Select record by passing a date within the range

php - 来自链接 php 的电子邮件验证

mysql - 将 MYSQL 转换为 Codeigniter

sql - 从 WHERE Postgresql 获取数据

c# - 如何使用 "Group by"LINQ?

Mysql,有没有办法知道在多个条件WHERE中哪些条件返回true?

PHP 和 mysql 表

php artisan 迁移错误 : nodename nor servname provided or not know

mysql - Mysql 中使用 count、group by、order by 和 union 进行选择失败