简体   繁体   English

具有多个GROUP BY的MySQL查询-如何在HTML表格中将一组设为列头,一组设为行头?

[英]MySQL Query with multiple GROUP BY - How do I make one group a column head and one group a row head in an HTML table?

That title may not make sense, I'm not sure how to ask what I'm trying to do in a single sentence... 该标题可能没有意义,我不确定如何用一个句子问我要做什么...

I have a MySQL table like this: 我有一个这样的MySQL表:

| id | user  | team   | month | result |
|----|-------|--------|-------|--------|
| 1  | Joe   | red    | sept  | 100    |
| 2  | Joe   | red    | oct   | 40     |
| 3  | Jim   | red    | sept  | 70     |
| 4  | Jim   | red    | oct   | 50     |
| 5  | Susy  | red    | sept  | 40     |
| 6  | Tim   | blue   | sept  | 60     |
| 7  | Tim   | blue   | oct   | 100    |
| 8  | Betty | blue   | sept  | 70     |
| 9  | Dave  | blue   | sept  | 20     |
| 10 | Stan  | green  | oct   | 40     |
| 11 | Alan  | green  | sept  | 80     |
| 12 | Tina  | green  | oct   | 100    |
| 13 | Tina  | green  | sept  | 30     |
| 14 | Rick  | yellow | oct   | 50     |
| 15 | Ellen | yellow | oct   | 60     |

Ultimately I'm trying to output an HTML table that shows a total number of users who have a result greater than 50 organized by team and month. 最终,我试图输出一个HTML表,该表显示按团队和月份组织的结果大于50的用户总数。

Here's the query I'm running right now: 这是我现在正在运行的查询:

SELECT team, month, count(*)
FROM example
WHERE result >= 50
GROUP BY team, month

Which returns this: 哪个返回:

| team   | month | count(*) |
|--------|-------|----------|
| blue   | oct   | 1        |
| blue   | sept  | 2        |
| green  | oct   | 1        |
| green  | sept  | 1        |
| red    | oct   | 1        |
| red    | sept  | 2        |
| yellow | oct   | 2        |

But in my HTML table I want to list out months as columns (and add a total column). 但是在我的HTML表中,我想以列的形式列出月份(并添加总计列)。 So an HTML table that renders like this (ignore the sorting, it's arbitrary): 因此,这样呈现的HTML表(忽略排序,它是任意的):

| Team   | sept | oct | Total |
|--------|------|-----|-------|
| red    | 2    | 1   | 3     |
| blue   | 2    | 1   | 3     |
| green  | 1    | 1   | 2     |
| yellow | 0    | 2   | 2     |

Can I work with the query result I have and somehow manipulate it into the end format in the PHP/HTML? 我可以使用已有的查询结果,并以某种方式将其处理为PHP / HTML的最终格式吗? Or do I need to change the way I'm querying in the first place? 还是我首先需要更改查询方式?

*Editing to change the HTML table output example - I want this to be dynamic so that as more months are added to original table the resulting HTML table can expand horizontally. *编辑以更改HTML表输出示例-我希望这是动态的,以便将更多的月份添加到原始表中,结果HTML表可以水平扩展。

The basic SQL you need is: 您需要的基本SQL是:

SELECT  Team,
        COUNT(CASE WHEN Month = 'Sept' THEN 1 END) AS Sept,
        COUNT(CASE WHEN Month = 'Oct' THEN 1 END) AS oct,
        COUNT(*) AS Total
FROM    T
GROUP BY Team;

Example on SQL Fiddle SQL小提琴示例

If you don't know the number of columns you need you will need to do it dynamically with a prepared statement' 如果您不知道所需的列数,则需要使用准备好的语句动态地进行操作。

SET @sql = NULL;
SELECT  GROUP_CONCAT(DISTINCT
            CONCAT(
                'COUNT(CASE WHEN Month = ''',
                Month,
                ''' THEN 1 END) AS ',
                Month
                )
            ) 
INTO    @sql
FROM    T;

SET @sql = CONCAT('SELECT Team, ', @sql, ', COUNT(*) AS Total FROM T GROUP BY Team');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Example on SQL Fiddle SQL小提琴示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM