在 mysql 中计算每天的出现次数

count ocurrences over each day in mysql

我在弄清楚如何解决时遇到了一些问题,但我无法在 all.This 找到答案。

我有一个 mySQL table 如下所示:

cust_id,date_removed,station_removed,date_arrived,station_arrived
6,"2010-02-02 13:57:00",56,"2010-02-02 13:58:00",77
6,"2010-02-02 15:12:00",66,"2010-02-02 15:12:00",56
30,"2010-02-05 11:36:00",32,"2010-02-05 11:37:00",14
30,"2010-02-05 11:37:00",14,"2010-02-05 11:37:00",20
30,"2010-02-05 12:41:00",85,"2010-02-05 12:43:00",85
30,"2010-02-05 12:44:00",85,"2010-02-05 12:46:00",85
30,"2010-02-06 13:15:00",8,"2010-02-06 13:17:00",20
30,"2010-02-06 13:18:00",23,"2010-02-06 13:19:00",23
30,"2010-02-06 13:20:00",32,"2010-02-06 13:21:00",39
30,"2010-02-06 13:21:00",11,"2010-02-06 13:21:00",23
30,"2010-02-06 13:21:00",76,"2010-02-06 13:22:00",32

其中每个字段对应的数据类型如下: cust_id: 变量() date_removed: 日期时间 station_removed: 整数 date_arrived: 日期时间 station_arrived: 整数

接下来,我被要求进行查询以获取一天中使用的每个站点的计数,得到一个像这样的 table:

station   2010-02-02   2010-02-05  2010-02-06
56            2             0           0
66            1             0           0
32            0             1           2
14            0             2           0
85            0             2           0
8             0             0           1
23            0             0           2
11            0             0           1
76            0             0           1
77            1             0           0
20            0             1           1
39            0             0           1

其中列是天数,行是每个站点。我也不是一个很好的 mySQL 用户。

有人可以帮我解决这个问题吗?

提前致谢

使用这个查询:

 select stations.name as station, 

 (select count(*) from table where date(date_arrived)='2010-02-02' and (station_removed=stations.name or station_arrived=stations.name)) as '2010-02-02'
 (select count(*) from table where date(date_arrived)='2010-02-05' and (station_removed=stations.name or station_arrived=stations.name)) as '2010-02-05'
 (select count(*) from table where date(date_arrived)='2010-02-06' and (station_removed=stations.name or station_arrived=stations.name)) as '2010-02-06'

 from

(select station_removed as name from table
  union
 select station_arrived from table ) stations ;