查询中的MySQL小计

时间:2014-09-22 21:31:35

标签: mysql

我已经非常怀疑这个问题了!

我有这个查询

SELECT Player.MembershipNo , Player.FirstName , Player.LastName
     , Venue.VenueName as Venue, Results.MemCard, Results.EarlyReg
     , Position.Points as Venue_Points, Results.Date
FROM Position , Player , Results , Venue 
WHERE Player.MembershipNo =Results.MembershipNo 
  AND Results.Position =Position.Position 
  AND Venue.VenueID =Results.VenueID 
  AND Results.Date BETWEEN '2014-07-01' AND '2014-09-30' 
ORDER BY MembershipNo, Venue

返回这些结果:

+--------------+-----------+----------+-------------------+---------+----------+--------------+------------+
| MembershipNo | FirstName | LastName |       Venue       | MemCard | EarlyReg | Venue_Points |    Date    |
+--------------+-----------+----------+-------------------+---------+----------+--------------+------------+
|            0 | Bob       | Stevens  | The Dolphin       |       1 |        1 |           32 | 27/08/2014 |
|            0 | Bob       | Stevens  | The Enigma Tavern |       1 |        1 |           40 | 08/07/2014 |
|            0 | Bob       | Stevens  | The Enigma Tavern |       1 |        1 |           16 | 15/07/2014 |
|            1 | Dave      | Green    | The Dolphin       |       1 |        1 |           20 | 13/08/2014 |
|            1 | Dave      | Green    | The Dolphin       |       1 |        1 |            2 | 20/08/2014 |
+--------------+-----------+----------+-------------------+---------+----------+--------------+------------+

我还想查看会员号码在结果表中显示的总次数,而不是COUNT DISTINCT,因为这会给我这个:

+--------------+-----------------+-----------+----------+-------------+---------+----------+--------------+------------+
| MembershipNo | MembershipCount | FirstName | LastName |    Venue    | MemCard | EarlyReg | Venue_Points |    Date    |
+--------------+-----------------+-----------+----------+-------------+---------+----------+--------------+------------+
|            0 |               3 | Bob       | Stevens  | The Dolphin |       1 |        1 |           32 | 27/08/2014 |
|            1 |               2 | Dave      | Green    | The Dolphin |       1 |        1 |           20 | 13/08/2014 |
+--------------+-----------------+-----------+----------+-------------+---------+----------+--------------+------------+

我想要的是原始数据,其中每个行的成员资格计数如下:

+--------------+-----------------+-----------+----------+-------------------+---------+----------+--------------+------------+
| MembershipNo | MembershipCount | FirstName | LastName |       Venue       | MemCard | EarlyReg | Venue_Points |    Date    |
+--------------+-----------------+-----------+----------+-------------------+---------+----------+--------------+------------+
|            0 |               3 | Bob       | Stevens  | The Dolphin       |       1 |        1 |           32 | 27/08/2014 |
|            0 |               3 | Bob       | Stevens  | The Enigma Tavern |       1 |        1 |           40 | 08/07/2014 |
|            0 |               3 | Bob       | Stevens  | The Enigma Tavern |       1 |        1 |           16 | 15/07/2014 |
|            1 |               2 | Dave      | Green    | The Dolphin       |       1 |        1 |           20 | 13/08/2014 |
|            1 |               2 | Dave      | Green    | The Dolphin       |       1 |        1 |            2 | 20/08/2014 |
+--------------+-----------------+-----------+----------+-------------------+---------+----------+--------------+------------+

我有什么想法可以做到这一点吗?

1 个答案:

答案 0 :(得分:0)

似乎是一种奇怪的方法,可以像第二个查询中那样绑定累计计数,并将其与未聚合的记录数据一起列出。但是,您可以通过子选择获取聚合数据,然后将该子选择加入类似于第一个查询的查询中。这可能看起来像这样:

SELECT
    agg.member_count,
    /* other fields from your first query here */
FROM
   (
   /* second query with aggregation total here.. I am assuming you allias the count field as member_count
   */
   ) AS agg
   INNER JOIN Player ON agg.MembershipNo = Player.MembershipNo
   /* make other table joins here as done in first query */
WHERE /* filter */
ORDER BY /* order */