mysql查询做排名功能

时间:2016-10-10 10:13:19

标签: mysql dense-rank mysql-variables

我想写一个MySQL查询来排名分数。如果两个分数之间存在平局,则两者应具有相同的排名。在平局之后,下一个排名数字应该是下一个连续的整数值。换句话说,应该没有"漏洞"在队伍之间。

+-------+------+
| Score | Rank |
+-------+------+
| 4.00  | 1    |
| 4.00  | 1    |
| 3.85  | 2    |
| 3.65  | 3    |
| 3.65  | 3    |
| 3.50  | 4    |
+-------+------+

根据上述分数表,查询应生成以下报告(按最高分数排序)

SELECT  score,Rank
 FROM
 (
  SELECT  score,
      @rn1 := if(@pk1=score,@rn1, @rn1+@val) as Rank,
      @val := if(@pk1=score,@val+1, 1 ) as value,
      @pk1 := score

  FROM
(SELECT  score FROM scores ORDER BY score) as rank) as scores;

我尝试过类似下面的内容     设置@ pk1 ='&#39 ;;     设置@ rn1 = 1;     设置@val = 1;

[alert setModalPresentationStyle:UIModalPresentationPopover];

    UIView *view = [self.tabBar.selectedItem valueForKey:@"view"];

    alert.popoverPresentationController.delegate = self;
    alert.popoverPresentationController.sourceView = self.tabBar;
    alert.popoverPresentationController.sourceRect = view.frame;
    alert.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionDown;

    [self presentViewController:alert animated:YES completion:nil];

但是上面没有任何工作,任何人都可以帮助这个

0 个答案:

没有答案