简体   繁体   English

如何在PHP中按年份计算一栏中的项目数

[英]How to count number of Items in a column by year in PHP

Is this possible? 这可能吗? I have the following table 我有下表

ID   CAR  MODEL  YEAR   FUELTYPE
1    Volvo S80   2007        GAS
2    Volvo S80D  2007        DIESEL
3    Volvo S80E  2009        LPN

What I want to do is to count the different FUELTYPE's by year, the output would be something like this, this is the format for the Google Chart I want to use. 我想做的是按年份计算不同的FUELTYPE,输出将是这样,这是我要使用的Google图表的格式。

Year GAS DIESEL LPN
2007  1     1    0
2009  0     0    1

How can I do it with MySQL? 我该如何使用MySQL? Can I do something like this? 我可以做这样的事情吗?

SELECT YEAR, COUNT(FUELTYPE) AS GAS, COUNT (FUELTYPE) AS DIESEL, COUNT (FUELTYPE) AS LPN
FROM  CARDATA
GROUP BY FUELTYPE
ORDER BY YEAR

I know it is not right as I get the same count value in each FUELTYPE - is it better to just pull it out of mysql and then use php to sort/count? 我知道这是不对的,因为我在每个FUELTYPE中获得相同的计数值-最好只是将其从mysql中拉出,然后使用php进行排序/计数吗?

You can use an aggregate function with a CASE expression to pivot the row data into columns: 您可以将聚合函数与CASE表达式一起使用,以将行数据透视成列:

select year,
  sum(case when fueltype = 'gas' then 1 else 0 end) gas,
  sum(case when fueltype = 'diesel' then 1 else 0 end) diesel,
  sum(case when fueltype = 'lpn' then 1 else 0 end) lpn
from yt
group by year

See SQL Fiddle with Demo . 请参阅带有演示的SQL Fiddle

If you have an unknown number of FuelType values, then you can use a prepared statement to generate dynamic SQL: 如果未知数量的FuelType值,则可以使用准备好的语句来生成动态SQL:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'sum(CASE WHEN fueltype = ''',
      fueltype,
      ''' THEN 1 else 0 END) AS `',
      fueltype, '`'
    )
  ) INTO @sql
FROM yt;

SET @sql 
  = CONCAT('SELECT year, ', @sql, ' 
            from yt
            group by year');

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

See SQL Fiddle with Demo . 请参阅带有演示的SQL Fiddle Both this the result: 这两个结果:

| YEAR | GAS | DIESEL | LPN |
-----------------------------
| 2007 |   1 |      1 |   0 |
| 2009 |   0 |      0 |   1 |

If there are only three FuelType , the easiest solution on this in MySQL is by using GROUP BY , and SUM() . 如果只有三个FuelType ,则MySQL最简单的解决方案是使用GROUP BYSUM()

SELECT  Year,
        SUM(FUELTYPE = 'GAS') GAS,
        SUM(FUELTYPE = 'DIESEL') DIESEL,
        SUM(FUELTYPE = 'LPN') LPN
FROM    CARDATA
GROUP   BY YEAR

but if you have unknown number of FUELTYPE , a dynamic sql is more prefereed. 但是如果您不知道FUELTYPE数量, FUELTYPE使用动态sql。

SET @sql = NULL;

SELECT  GROUP_CONCAT(DISTINCT
        CONCAT('SUM(FUELTYPE = ''',
               FUELTYPE, ''') AS ',
               CONCAT('`', FUELTYPE, '`')
               )) INTO @sql
FROM CARDATA;

SET @sql = CONCAT('SELECT   Year, ', @sql, ' 
                   FROM CARDATA
                   GROUP BY YEAR');

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

OUTPUT 输出值

╔══════╦═════╦════════╦═════╗
║ YEAR ║ GAS ║ DIESEL ║ LPN ║
╠══════╬═════╬════════╬═════╣
║ 2007 ║   1 ║      1 ║   0 ║
║ 2009 ║   0 ║      0 ║   1 ║
╚══════╩═════╩════════╩═════╝

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

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