3

简单问题:为什么以下查询不输出perday

SELECT FROM_UNIXTIME(`date`,"%Y-%m-%d") AS `perday`, COUNT(*) AS `count` 
FROM `data` 
WHERE `group` = 1
GROUP BY `perday`

计数正确输出,但perday保持为空。表data是这样的:

 |   id   |   group   |          date          |
------------------------------------------------
 |   1    |     1     |  2013-04-13 06:01:02   |
 |   2    |     1     |  2013-04-13 14:24:18   |
 |   3    |     2     |  2012-01-21 21:33:03   |
              Ect.

谢谢!

编辑:

预期输出:

 |   perday   |
 --------------
 | 2013-04-13 |
 | 2012-01-21 |
4

3 回答 3

6

删除WHERE子句,

SELECT  FROM_UNIXTIME(date,'%Y-%m-%d') AS perday, 
        SUM(`group` = 1)  AS `count` 
FROM    data 
GROUP   BY FROM_UNIXTIME(date,'%Y-%m-%d')

如果日期格式为2013-04-13 06:01:02,那么为什么要使用FROM_UNIXTIME?不是DATE_FORMAT相反吗?

SELECT  DATE_FORMAT(date, '%Y-%m-%d') AS perday, 
        SUM(`group` = 1)  AS `count` 
FROM    data 
GROUP   BY DATE(date)
ORDER   BY date

这将在表格中显示所有可用日期。

但如果你group只想要选定的,

SELECT  DATE_FORMAT(date, '%Y-%m-%d') AS perday, 
        COUNT(*)  AS `count` 
FROM    data 
WHERE   `group` = 1
GROUP   BY DATE(date)
ORDER   BY date
于 2013-06-01T09:27:58.303 回答
0

你可能会找这个

    SELECT DATE_FORMAT(`date`,"%Y-%m-%d") AS `perday`, COUNT(*) AS `count` 
    FROM `data` 
    WHERE `group` = 1
    GROUP BY `perday`

在这里演示

于 2013-06-01T09:32:32.413 回答
0

MySQL从表达式DATE()中取出 DATE 部分。因为你的这个查询很好。DATETIMEExpected output

SELECT
  DATE(`date`) AS `perday`,
  COUNT(*) AS `count`
FROM `data`
GROUP BY `perday`
于 2013-06-01T09:34:35.803 回答