1

我正在尝试使用以下查询编写报告:

SELECT
    DATE_FORMAT(date, '%Y-%m-%d') AS 'Date',
    CONCAT(UCASE(MID(name,1,1)),MID(name,2)) AS 'Username',
    COUNT(admin_id) AS 'Surveys Carried Out'
FROM
    `customer_surveys`
LEFT JOIN `admin` ON customer_surveys.admin_id = admin.adminid
GROUP BY DATE_FORMAT(date, '%Y-%m-%d')

它需要按日期(因为它是逐日报告)和 admin_id(因为它是对该 ID 进行了多少调查的计数)进行分组

这在理论上应该有效。但是看看它返回的内容,并将其与实际数据进行比较,它每天都在显示它,但会合并 admin_ids,因此当天第一个出现的就是显示的那个。有什么帮助吗?

4

3 回答 3

3

如果要显示每个管理员的日期记录,则必须至少按日期和 admin_id 对其进行分组

GROUP BY DATE_FORMAT(date, '%Y-%m-%d'), admin_id
于 2013-02-04T15:03:24.403 回答
1

听起来您只需要添加一个GROUP BY admin_id

SELECT
    DATE_FORMAT(date, '%Y-%m-%d') AS 'Date',
    CONCAT(UCASE(MID(name,1,1)),MID(name,2)) AS 'Username',
    COUNT(admin_id) AS 'Surveys Carried Out'
FROM `customer_surveys`
LEFT JOIN `admin` 
   ON customer_surveys.admin_id = admin.adminid
GROUP BY DATE_FORMAT(date, '%Y-%m-%d'), admin_id

按两者分组将为您提供DATE_FORMAT(date, '%Y-%m-%d')admin_id

于 2013-02-04T15:03:34.393 回答
1

您不需要 group by 中的所有非聚合体吗?Concat 不是聚合体

SELECT
    DATE_FORMAT(date, '%Y-%m-%d') AS 'Date',
    CONCAT(UCASE(MID(name,1,1)),MID(name,2)) AS 'Username',
    COUNT(admin_id) AS 'Surveys Carried Out'
FROM
    `customer_surveys`
LEFT JOIN `admin` ON customer_surveys.admin_id = admin.adminid
GROUP BY DATE_FORMAT(date, '%Y-%m-%d'), CONCAT(UCASE(MID(name,1,1)),MID(name,2))
于 2013-02-04T15:04:06.243 回答