mysql - 统计mysql中每天的出现次数

标签 mysql mysql-workbench

我遇到了一些问题,不知道如何解决,但我根本无法给出答案。这是我的问题。

我有一个如下所示的 mySQL 表:

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:varchar() date_removed:日期时间 station_removed:int date_arrived: 日期时间 station_arrived:整数

接下来,我被要求进行查询以获取当天使用的每个电台的计数,以获得如下所示的表格:

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 ;

关于mysql - 统计mysql中每天的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30008063/

相关文章:

mysql - 我想获取 2 个日期之间的数据,但显示 1064 错误

mysql 当找到 LIKE 时选择 0 或 1

php - 如何添加编号或项目符号?

php - 如何在没有任何交互的情况下从数据库中检索数据?

MySQL Workbench 不会连接到 AWS RDS 数据库。错误 - "Unable to connect to localhost"

mysql - 为什么 "forward engineering"功能在 mySQL 工作台上不起作用?

php - 递归获取树的所有 parent 和 child

mysql - mysql查询中的正则表达式

MySQL Workbench 不尊重配置的字符集 utf8mb4

mysql - Insert into.....values((select 语句), value1, value2, value3) Select 语句返回多列