MySQL查询过去30天排序

时间:2016-02-19 22:20:24

标签: mysql sql

我有一个查询需要修改才能使用数据变量来搜索从今天开始的最近30天和按日期分组的计数。我的SQL服务器的/tmp/目录(15GB)不断填满,查询失败。

 SELECT DATE(`date_time`) AS DAY
, COUNT(DISTINCT(rcid)) AS COUNT
, COUNT(DISTINCT(tunnelip)) AS TAILS 
FROM primarydata 
WHERE SERVER LIKE"%VOE%" 
AND DATE_SUB(NOW(), INTERVAL 30 DAY) and NOW() 
GROUP BY DAY;

4 个答案:

答案 0 :(得分:0)

您可以在shell中手动运行查询吗?看起来你几乎就在那里。它可能会失败。在执行的这一点上,它不知道DAY是什么列。

尝试:GROUP BY DATE(date_time

而不是:GROUP BY DAY。

答案 1 :(得分:0)

试试这个:

SELECT
  DATE(`date_time`)          AS DAY,
  COUNT(DISTINCT (rcid))     AS COUNT,
  COUNT(DISTINCT (tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE
      "%VOE%" AND `date_time` BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
GROUP BY `date_time`;

答案 2 :(得分:0)

您的日期检查缺少要测试的列名,也缺少两个日期之间的BETWEEN关键字。

SELECT
  DATE(`date_time`)          AS DAY,
  COUNT(DISTINCT (rcid))     AS COUNT,
  COUNT(DISTINCT (tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE "%VOE%" 
    AND date_time BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
GROUP BY DAY;

答案 3 :(得分:0)

谢谢大家。我无法使用CURDATE()

工作
 AND `date_time` BETWEEN CURDATE() - INTERVAL 30
SELECT
  DATE(`date_time`)          AS DAY,
  COUNT(DISTINCT (rcid))     AS COUNT,
  COUNT(DISTINCT (tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE "%VOE%" AND `date_time` BETWEEN CURDATE() - INTERVAL 30 DAY AND NOW() GROUP BY DAY;