返回count()日匹配mysql

时间:2018-11-17 03:23:25

标签: mysql

我有这张桌子,其中id_user = 7

Form1_Load

预期输出

require 'win32/screenshot'
Win32::Screenshot::Take.of(:foreground).write("Screenshot.png")

我想返回用户每天出现的时间

到目前为止,我已经尝试过...

7    2018-11-12
7    2018-11-12
7    2018-11-13
7    2018-11-13
7    2018-11-13
7    2018-11-13
7    2018-11-14
7    2018-11-15
7    2018-11-15
7    2018-11-15
7    2018-11-15
7    2018-11-15
7    2018-11-16
7    2018-11-16
7    2018-11-16
7    2018-11-16
7    2018-11-16
7    2018-11-16
7    2018-11-16

2 个答案:

答案 0 :(得分:1)

您没有在查询中输出日期,也没有在id_user上限制结果。试试这个:

SELECT `date`, COUNT(*)
FROM posts
WHERE id_user = 7
GROUP BY `date`

输出

date        COUNT(*)
2018-11-12  2
2018-11-13  4
2018-11-14  1
2018-11-15  5
2018-11-16  7

Demo on dbfiddle

答案 1 :(得分:1)

查询:

 SELECT date, COUNT(*) AS Total
    FROM posts
    WHERE id_user = 7
    GROUP BY date

输出:

date       | Total
-----------------
2018-11-12 | 2
2018-11-13 | 4
2018-11-14 | 1
2018-11-15 | 5
2018-11-16 | 7

更新:

如果要更改日期格式,请使用以下代码,

SELECT DATE_FORMAT(date, "%d/%m/%Y") As date, COUNT(*) AS Total
        FROM posts
        WHERE id_user = 7
        GROUP BY date

输出:

date       | Total
-----------------
12/11/2018 | 2
13/11/2018 | 4
14/11/2018 | 1
15/11/2018 | 5
16/11/2018 | 7