一次分两列或?

时间:2013-02-04 14:58:46

标签: mysql

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

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,因此当天第一个出现的是显示的那个。有什么帮助吗?

3 个答案:

答案 0 :(得分:3)

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

对其进行分组
GROUP BY DATE_FORMAT(date, '%Y-%m-%d'), admin_id

答案 1 :(得分: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

的值

答案 2 :(得分:1)

你不需要群组中的所有非聚合吗? 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))