所需格式和表

时间:2016-03-22 13:01:19

标签: mysql

我有一个名为lumia的数据库,在该数据库中,我有几个表,用当前日期和时间更新。触发链接的时间。我最后给出了mysql转储文件的链接。

我想要以下格式的数据: -

+----------+------------+----------+
| TC       |      Play  |   Skip   |
+----------+------------+----------+
| blr      |        132 |    33    |
| Chen     |        133 |    3     |
+----------+------------+----------+

其中Play列将包含blr_play_dt和skip列的行数将包含blr_skip_dt的行数等等,TC将包含blr,chen等等我有以下mysql查询: -

 SELECT
     'blr_play' AS table_label,
     COUNT(*) AS table_size
 FROM
     blr_play
UNION
     SELECT
       'blr_skip' AS table_label,

        COUNT(*) AS table_size
    FROM
         blr_skip

但它给出了这样的结果

 +----------+------------+
 | TC       | table_size |
 +----------+------------+
 | blr_play |        132 |
 | blr_skip |         33 |
 | chen_play|        124 |
 | chen_skip|          2 |
 +----------+------------+

我想要这种格式的上述数据: -

+----------+------------+----------+
| TC       |      Play  |   Skip   |
+----------+------------+----------+
| blr      |        132 |    33    |
| Chen     |        124 |    2     |
+----------+------------+----------+

sql转储文件的链接: - https://www.mediafire.com/?444n3tvcvj24dpm

1 个答案:

答案 0 :(得分:2)

您可以使用子查询执行此操作:

 SELECT
      'blr' AS table_label,
      COUNT(*) AS playCnt,
      (select count(*) from blr_skip) as skipCnt
FROM  blr_play

看起来每个TC都有一张桌子?所以对于陈也:

 SELECT
      'blr' AS table_label,
      COUNT(*) AS playCnt,
      (select count(*) from blr_skip) as skipCnt
FROM  blr_play
UNION ALL
 SELECT
      'Chen' AS table_label,
      COUNT(*) AS playCnt,
      (select count(*) from Chen_skip) as skipCnt
FROM  Chen_play

编辑:听起来你正在寻找条件聚合 -

 SELECT
      'blr' AS table_label,
      COUNT(CASE WHEN YourColumn ='2016-03-19' then 1 end) AS playCnt,
      (select count(CASE WHEN YourColumn ='2016-03-19' then 1 end) from blr_skip) as skipCnt
FROM  blr_play

当它实际上只是过滤那些结果(会表现得更好)

 SELECT
      'blr' AS table_label,
      COUNT(*) AS playCnt,
      (select count(*) from blr_skip WHERE YourColumn ='2016-03-19') as skipCnt
FROM  blr_play 
WHERE YourColumn ='2016-03-19'