1

我正在尝试构建一个查询,该查询仅根据日期获取一组记录的最新记录。

表的布局如下:

| date | category | action | label | label2 | count_today | count_total | period |

主键基于列date, category, action, label, label2, perioddate有 formatyyyy-mm-dd和 period 可以有值Day, Week, month.

对于每个独特的组合,category | action | label | label2我需要拥有最新日期的记录。

我的第一次尝试是这样的:

SELECT * FROM `statistic` 
WHERE 
 (action='total' OR action='' OR category='user')
 AND
 (period='day' 
   OR (period='week' AND DATEDIFF(now(), `date`) > 30)
   OR (period = 'Month' AND DATEDIFF(now(), `date`) > 7*26)
 )
GROUP BY category, action, label, label2
ORDER BY date DESC

此查询的问题在于它在 ORDER BY 之前执行 GROUP BY,导致返回不正确的记录。

经过搜索,我发现我想要的是所谓的分组最大查询。

我的下一次尝试是这样的:

SELECT s1.* FROM `statistic` AS s1
LEFT JOIN statistic AS s2 
ON 
 s1.category = s2.category
 AND s1.action = s2.action
 AND s1.label = s2.label
 AND s1.label2 = s2.label2
 AND s1.date > s2.date
WHERE 
   (s1.action='total' OR s1.action='' OR s1.category='user')
   AND
   (s1.period='day' 
   OR (s1.period='week' AND DATEDIFF(now(), s1.`date`) > 30)
   OR (s1.period = 'Month' AND DATEDIFF(now(), s1.`date`) > 7*26)
 )
GROUP BY category, action, label, label2

但是这个查询也没有给我正确的结果(它看起来类似于第一个查询)。

有什么线索可以获取我需要的数据吗?

4

1 回答 1

4

你是对的,你想要group-wise maximum,但是你可以通过将你的表与一个查找每个组的最新日期的子查询连接起来来实现这一点:

SELECT * FROM statistic NATURAL JOIN (
  SELECT   category, action, label, label2, MAX(date) date
  FROM     statistic
  GROUP BY category, action, label, label2
) t

然后,如果仍然需要以下过滤器:

WHERE 
 (action='total' OR action='' OR category='user')
 AND
 (period='day' 
   OR (period='week' AND DATEDIFF(now(), `date`) > 30)
   OR (period = 'Month' AND DATEDIFF(now(), `date`) > 7*26)
 )
于 2012-11-19T11:00:55.377 回答