简体   繁体   中英

Count and select all dates for a specific field in MySQL

i have a data format like this:

+----+--------+---------------------+
| ID | utente | data                |
+----+--------+---------------------+
| 1  | Man1   | 2014-02-10 12:12:00 |
+----+--------+---------------------+
| 2  | Women1 | 2015-02-10 12:12:00 |
+----+--------+---------------------+
| 3  | Man2   | 2016-02-10 12:12:00 |
+----+--------+---------------------+
| 4  | Women1 | 2014-03-10 12:12:00 |
+----+--------+---------------------+
| 5  | Man1   | 2014-04-10 12:12:00 |
+----+--------+---------------------+
| 6  | Women1 | 2014-02-10 12:12:00 |
+----+--------+---------------------+

I want to make a report that organise the ouptout in way like this:

+---------+--------+-------+---------------------+---------------------+---------------------+
| IDs     | utente | count | data1               | data2               | data3               |
+---------+--------+-------+---------------------+---------------------+---------------------+
| 1, 5    | Man1   | 2     | 2014-02-10 12:12:00 | 2014-04-10 12:12:00 |                     |
+---------+--------+-------+---------------------+---------------------+---------------------+
| 2, 4, 6 | Women1 | 3     | 2015-02-10 12:12:00 | 2014-03-10 12:12:00 | 2014-05-10 12:12:00 |
+---------+--------+-------+---------------------+---------------------+---------------------+

All the row thath include the same user (utente) more than one time will be included in one row with all the dates and the count of records. Thanks

While it's certainly possible to write a query that returns the data in the format you want, I would suggest you to use a GROUP BY query and two GROUP_CONCAT aggregate functions:

SELECT
  GROUP_CONCAT(ID) as IDs,
  utente,
  COUNT(*) as cnt,
  GROUP_CONCAT(data ORDER BY data) AS Dates
FROM
  tablename
GROUP BY
  utente

then at the application level you can split your Dates field to multiple columns.

Looks like a fairly standard "Breaking" report, complicated only by the fact that your dates extend horizontally instead of down...

SELECT * FROM t ORDER BY utente, data
$lastutente = $lastdata = '';
echo "<table>\n";
while ($row = fetch()) {
    if ($lastutente != $row['utente']) {
        if ($lastutente != '') {
            /****
             * THIS SECTION REF'D BELOW
             ***/
            echo "<td>$cnt</td>\n";
            foreach ($datelst[] as $d) 
                echo "<td>$row[data]</td>\n";
            for ($i = count($datelst); $i < $NumberOfDateCells; $i++)
                echo "<td>&nbsp;</td>\n";
            echo "</tr>\n";
            /****
             * END OF SECTION REF'D BELOW
             ***/
        }
        echo "<tr><td>$row[utente]</td>\n"; // start a new row - you probably want to print other stuff too
        $datelst = array();
        $cnt = 0;
    }
    if ($lastdata != $row['data']) {
        datelst[] = $row['data'];
    }
    $cnt += $row['cnt']; // or $cnt++ if it's one per row
}
print the end of the last row - see SECTION REF'D ABOVE
echo "</table>\n";

You could add a GROUP BY utente, data to your query above to put a little more load on mysql and a little less on your code - then you should have SUM(cnt) as cnt or COUNT(*) as cnt .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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