如何在sql

时间:2016-06-03 08:49:24

标签: php mysql sql

我有数据库,其中存储了用户设备ID和设备注册日期(cr_date)我希望在同一天以日期方式获取已注册设备总数的列表

下面显示表格结构

device_ID   | cr_date 
--------------------
    1       | 2016-06-02 18:02:13   
    2       | 2016-06-02 18:03:58
    3       | 2016-06-02 18:04:11
    4       | 2016-06-03 18:04:33
    5       | 2016-06-03 18:04:33
    6       | 2016-06-04 18:04:44 

我希望得到以下结果

count   |  date 
---------------------------------
    3   |   2016-06-02
    2   |   2016-06-03
    1   |   2016-06-04 

我使用下面的查询但是获得成功

SELECT device_type, count(*) AS duplicate_count
FROM (
 SELECT device_type FROM tbl_app_deviceregister
 GROUP BY device_type HAVING COUNT(device_type) > 0
) AS t

4 个答案:

答案 0 :(得分:0)

您应该GROUP BY日期值,而不是设备:

SELECT CAST(cr_date AS DATE) AS `date`, COUNT(device_id) 
FROM tbl_app_deviceregister
GROUP BY CAST(cr_date AS DATE)

答案 1 :(得分:0)

你想要这个吗?)

SELECT count(1) as `count`, date_format(cr_date, '%Y-%m-%d') as `date`
FROM tbl_app_deviceregister
GROUP BY `date`
ORDER BY `count` DESC

答案 2 :(得分:0)

您的查询似乎没有引用您所描述的相同表结构,但根据我的理解,这会有效吗?

SELECT count(id), date_registered FROM (
    SELECT 
        id, 
        DATE(FROM_UNIXTIME(cr_date)) as date_registered 
    FROM 
        tbl_app_deviceregister
) A GROUP BY date_registered

答案 3 :(得分:0)

你按照device_type对它进行分组,这是错误的。尝试按日期分组。

select COUNT(*) as count,CONVERT(date,cr_date) as date
from tbl_app_deviceregister
group by CONVERT(date,cr_date)