0

我有 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)

我需要获取每种类型的最后日期。我怎样才能在一个查询中得到它?

谢谢你。

4

2 回答 2

4

这一定是 #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
于 2013-07-11T16:40:48.083 回答
0
SELECT u1.id, u1.type, u1.date, u1.like, u1.dislike
from updates u1
inner join
(
   select type, max(date) as mdate
   from updates
   group by type
) u2 on u2.type = u1.type 
     and u2.mdate = u1.date
于 2013-07-11T16:40:39.767 回答