我需要为10天的间隔计算单个用户的访问次数。
我总是看到上次访问的日期。
我已经尝试过这个查询,但它有问题。
mysql> SELECT
DATE_FORMAT(MIN(`theDate`), '%d/%m/%Y') AS 'First Access',
DATE_FORMAT(MAX(`theDate`), '%d/%m/%Y') AS 'Last Access',
COUNT(*) AS 'Number',
TheUser
FROM
`doTable`
WHERE
`theDate` BETWEEN MAKEDATE(YEAR(CURDATE()),1)
AND DATE_SUB(MIN(`theDate`), INTERVAL 10 DAY)
GROUP BY
TheUser
HAVING
COUNT(*) > 1
ORDER BY
COUNT(*) DESC;
1111 - Invalid use of group function
如果我使用日期值,则查询有效。
mysql> SELECT
DATE_FORMAT(MIN(`theDate`), '%d/%m/%Y') AS 'First Access',
DATE_FORMAT(MAX(`theDate`), '%d/%m/%Y') AS 'Last Access',
COUNT(*) AS 'Number',
TheUser
FROM
`doTable`
WHERE
`theDate` BETWEEN '2015-01-01'
AND DATE_SUB('2015-01-22', INTERVAL 10 DAY)
GROUP BY
TheUser
HAVING
COUNT(*) > 1
ORDER BY
COUNT(*) DESC;
输出:
+---------+------------+------------+--------+
| TheUser | First | Last | Number |
+---------+------------+------------+--------+
| s074 | 05/01/2015 | 05/01/2015 | 18 |
| x066 | 02/01/2015 | 12/01/2015 | 17 |
| f918 | 03/01/2015 | 03/01/2015 | 17 |
| l371 | 01/01/2015 | 02/01/2015 | 15 |
| b779 | 01/01/2015 | 02/01/2015 | 14 |
+---------+------------+------------+--------+
5 rows in set
编辑#1
我已将条件移动到having子句,但输出为空。
mysql> SELECT
DATE_FORMAT(MIN(`theDate`), '%d/%m/%Y') AS 'First Access',
DATE_FORMAT(MAX(`theDate`), '%d/%m/%Y') AS 'Last Access',
COUNT(*) AS 'Number',
TheUser,
`DATA`
FROM
`doTable`
GROUP BY
TheUser
HAVING
'Number' > 1
AND `theDate` BETWEEN MAKEDATE(YEAR(CURDATE()),1)
AND DATE_SUB(MIN(`DATA`), INTERVAL 10 DAY)
ORDER BY
'Number' DESC;