简体   繁体   English

计算每个状态在php和mysql中的日期范围内的条目总数

[英]Calculate total number of entries per state for a range of dates in php and mysql

I have users registered according to date from various states in my database. 我已经按照日期从数据库中各个状态注册了用户。

Q: I want to build a table which displays total users registered per state for a range of dates with totals column and row wise. 问:我想构建一个表,以显示每个州在一系列日期范围内按日期总计和按行总计的注册用户总数。 Is this possible? 这可能吗? something like this table below 如下表所示

    2013-10-01    2013-10-02    2013-10-03    2013-10-04    2013-10-05    TOTAL

NY:   125            110          102           98            75           510
MN:   21              56           78           41            22           218
CO:   17              41           52           51            56           217
TX:   10             102           45           62            21           240
NO:   59              85           78           48            98           368

TOTAL 232            394          355           300           272         1553 

I have been working on the following query 我一直在进行以下查询

if ($_GET) {
$dateF = date('Y-m-d', strtotime($_GET['datefrom']));
$dateT = date('Y-m-d', strtotime($_GET['dateto']));

$db = new PDO('mysql:host=localhost;dbname=database;charset=utf8', 'username', 'pass');

$stmt = $db->query("
    SELECT   `Date`, COUNT(*) AS `count_num`
    FROM     `table`
    WHERE    `Date` BETWEEN '$dateF' AND '$dateT'
    GROUP BY `Date`
    ORDER BY `Date` ASC
");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        echo "<tr>
                <td>" . $row["Date"] . "</td>
                <td>" . $row["count_num"] . "</td>

              </tr>";
        }

The above query only gives me data per date but for all the states combined. 上面的查询仅提供每个日期的数据,但包含所有状态的数据。 something like this 像这样的东西

Date         Total
2013-10-01    564
2013-10-02    124
2013-10-03    521 
etc..

I have tried working on the following query too 我也尝试过以下查询

$stmt = $db->query("SELECT state, COUNT(*) cnt1 FROM `table` WHERE Date between '$datefrom' AND '$dateto' GROUP BY state;");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>
   <td>" . $row["state"] . ":</td>
   <td>" . $row["cnt"] . "</td>
 </tr>";

} }

but this only displays total data per state for the date range entered. 但这只会显示输入日期范围内每个州的总数据。 something like this 像这样的东西

NY: 125
MN: 21
CO: 17
TX: 10
NO: 59

Any help greatly appreciated. 任何帮助,不胜感激。

You need something called pivot query . 您需要一种称为数据透视查询的东西。

A simple example --> http://www.sqlfiddle.com/#!2/6fc8b/1 一个简单的例子-> http://www.sqlfiddle.com/#!2/6fc8b/1

SELECT state,
       SUM( if( date='2013-10-01', 1, 0) ) `2013-10-01`,
       SUM( if( date='2013-10-02', 1, 0) ) `2013-10-02`, 
       SUM( if( date='2013-10-03', 1, 0) ) `2013-10-03`
       --  .........   
       --  .........  
       --  .........  
FROM `table`
GROUP BY state;

| STATE | 2013-10-01 | 2013-10-02 | 2013-10-03 |
|-------|------------|------------|------------|
|    CO |          4 |          2 |          3 |
|    NY |          3 |          1 |          0 |
|    TX |          1 |          0 |          4 |


SELECT `date`,
       SUM( CASE state WHEN 'NY' THEN 1 ELSE 0 END ) `NY`,
       SUM( CASE state WHEN 'CO' THEN 1 ELSE 0 END ) `CO`,
       SUM( CASE state WHEN 'TX' THEN 1 ELSE 0 END ) `TX`
       --  .........  
       --  .........  
       --  .........  
FROM `table`
GROUP BY `date`

|                           DATE | NY | CO | TX |
|--------------------------------|----|----|----|
| October, 01 2013 00:00:00+0000 |  3 |  4 |  1 |
| October, 02 2013 00:00:00+0000 |  1 |  2 |  0 |
| October, 03 2013 00:00:00+0000 |  0 |  3 |  4 |

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

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