代码之家  ›  专栏  ›  技术社区  ›  user794846

Mysql仅使用一个表为每个奖杯合计条目

  •  0
  • user794846  · 技术社区  · 7 年前

    我有一个包含以下字段的表:

    32, 1, hk57 hxz
    33, 1, hk57 hxz
    34, 1, sj2 ghd
    35, 2, hk57 hxz
    36, 2 sj2 ghd
    

    如何编写一个查询来汇总每辆车对每个奖杯的投票数?

    或者换一种方式,计算同一注册对同一奖杯的次数,并按奖杯显示?

    4 回复  |  直到 7 年前
        1
  •  0
  •   criw    7 年前

    我现在没有办法尝试,但我相信你只需要 group by 奖杯和 count 寄存器,类似于:

    SELECT trophy, registration, count(*) FROM your_table
    GROUP BY registration, trophy
    
    • count(*)
    • 分组依据 表示“并按此列将其分组”

    这里有一些参考资料:

    https://dev.mysql.com/doc/refman/5.7/en/counting-rows.html

    https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html


    SELECT registration, trophy, count(*) as votes FROM your_table
    GROUP BY registration, trophy
    

    所以count(*)列将被称为更漂亮的投票

        2
  •  0
  •   Barmar    7 年前

    由于您希望对每个注册和奖杯配对进行单独计数,请在中同时使用这两列 GROUP BY

    SELECT registration, trophy, COUNT(*) AS votes
    FROM yourTable
    GROUP BY registration, trophy
    ORDER BY trophy, registration
    
        3
  •  0
  •   Barmar    7 年前
    SELECT registration, COUNT(trophy) FROM table_name GROUP BY registration, trophy;
    
        4
  •  0
  •   Cheryl Velez    7 年前

    你需要根据奖杯和注册进行分组。

    SELECT count(*) as TotalVotes, trophy, registration FROM your_table 
    GROUP BY trophy, registration
    

        TotalVotes | trophy | registration
             2     |    1   | hk57 hxz
             1     |    1   | sj2 ghd
             1     |    2   | sj2 ghd
             1     |    2   | hk57 hxz