MySQL找到前三名排名的球队

时间:2016-10-15 15:57:17

标签: mysql

我需要以下列方式在我的排名页面上显示列表

a = "hello"
a = "b" + a[1:]  # ok, since we're making a new copy

print(a)  # prints "bello"
  

积分计算为

     
      
  • 2分/匹配获胜

  •   
  • 如果赢得的比赛是+1,则为+1

  •   
  • +2如果比赛获胜是半决赛

  •   
  • +3如果比赛获胜是一场大结局

  •   

我成功实现了大部分内容,但其中一个是In_Top_3,虽然有一个廉价的解决方法来添加Team_Name Played Won Lost Points In_Top_3 Some Team 5 5 0 11 Yes 并检查$count并在列下显示是但是有没有我可以使用SQL查询来完成它。

我当前的SQL查询如下

<=3

当我有以下表结构时,如何标记排名前3位的团队以及我当前的查询,

表:团队

SELECT
    T.id, T.name, T.status, IFNULL(T.image, 'no-image.png') AS DP,
    (SELECT COUNT(*)
    FROM badminton_matches MT
    WHERE (MT.team_one = T.id OR MT.team_two = T.id)) 
  AS played,
    (SELECT COUNT(*)
    FROM badminton_match_results R
    WHERE R.winner_id = T.id) AS won,
    (SELECT COUNT(*)
    FROM badminton_matches MT JOIN badminton_match_results MR
        ON (MR.match_id = MT.id)
    WHERE (MT.team_one = T.id OR MT.team_two = T.id) AND MR.winner_id != T.id) AS lost,
    (
   ((SELECT COUNT(*)
    FROM badminton_match_results R
    WHERE R.winner_id = T.id) * 2) 
     + 
   ((SELECT COUNT(*)
    FROM badminton_match_results R JOIN badminton_matches M ON (M.id = R.match_id AND M.match_type = 'quarter')
    WHERE R.winner_id = T.id))
  ) AS Points
FROM badminton_teams T
ORDER BY (Points) DESC

表:匹配

+------------+----------------------------+------+-----+-------------------+----------------+
| Field      | Type                       | Null | Key | Default           | Extra          |
+------------+----------------------------+------+-----+-------------------+----------------+
| id         | int(11)                    | NO   | PRI | NULL              | auto_increment |
| name       | varchar(150)               | NO   |     | NULL              |                |
| image      | text                       | YES  |     | NULL              |                |
| status     | enum('active','in-active') | NO   |     | active            |                |
| added_on   | datetime                   | NO   |     | CURRENT_TIMESTAMP |                |
| updated_on | datetime                   | YES  |     | NULL              |                |
+------------+----------------------------+------+-----+-------------------+----------------+

表:匹配结果

+------------+---------------------------------------+------+-----+-------------------+----------------+
| Field      | Type                                  | Null | Key | Default           | Extra          |
+------------+---------------------------------------+------+-----+-------------------+----------------+
| id         | int(11)                               | NO   | PRI | NULL              | auto_increment |
| team_one   | int(11)                               | NO   | MUL | NULL              |                |
| team_two   | int(11)                               | NO   |     | NULL              |                |
| added_on   | datetime                              | NO   |     | CURRENT_TIMESTAMP |                |
| match_type | enum('pool','quarter','semi','final') | NO   |     | pool              |                |
| sets       | smallint(2)                           | NO   |     | 1                 |                |
+------------+---------------------------------------+------+-----+-------------------+----------------+

1 个答案:

答案 0 :(得分:1)

这里有一个类似的问题:

How to add ROW INDEX as a column to SQL SELECT query?

从这个问题扩展到你想要的东西:

SET @row_num = 0;
SELECT
    T.id, T.name, T.status, IFNULL(T.image, 'no-image.png') AS DP,
    (SELECT COUNT(*)
    FROM badminton_matches MT
    WHERE (MT.team_one = T.id OR MT.team_two = T.id)) 
      AS played,
    (SELECT COUNT(*)
    FROM badminton_match_results R
    WHERE R.winner_id = T.id) AS won,
    (SELECT COUNT(*)
    FROM badminton_matches MT JOIN badminton_match_results MR
        ON (MR.match_id = MT.id)
    WHERE (MT.team_one = T.id OR MT.team_two = T.id) AND MR.winner_id != T.id) AS lost,
    (
       ((SELECT COUNT(*)
    FROM badminton_match_results R
    WHERE R.winner_id = T.id) * 2) 
         + 
       ((SELECT COUNT(*)
    FROM badminton_match_results R JOIN badminton_matches M ON (M.id = R.match_id AND M.match_type = 'quarter')
    WHERE R.winner_id = T.id))
      ) AS Points,

    /* here is the magic */
    (@row_num
:= @row_num + 1) < 4 AS row_index

    FROM badminton_teams T
    ORDER BY
(Points) DESC

这将添加一个名为row_index的额外列,其中1表示前3个,0表示不表示前3个。

请注意,您必须在每个SET之前和同一会话中致电SELECT