duanduo3712 2014-01-01 21:41
浏览 44
已采纳

使用where子句在mysql中按降序获取人员的等级

I have tried the http://sqlfiddle.com/#!2/df2ec/1 to get the data:I am trying to get the rank of name A which is 4 but I am unable to do it.

SELECT @rownum := @rownum + 1 AS rank, name, vote 
FROM uservotes, (SELECT @rownum := 0) t ORDER BY vote DESC

in data table:

create table uservotes(id int, name varchar(50), vote int);

INSERT INTO uservotes VALUES
(1, 'A', 34),
(2, 'B', 80),
(3, 'bA', 30),
(4, 'C', 8),
(5, 'D', 4),
(6, 'E', 14),
(7, 'F', 304),
(8, 'AA', 42),
(9, 'Ab', 6),
(10, 'Aa', 10);

I am unable to get the where clause .Basically I want to know which rank is the name based on the votes .I tried :

SELECT @rownum := @rownum + 1 AS rank, name, vote 
FROM uservotes WHERE name= A , (SELECT @rownum := 0) t ORDER BY vote DESC

But I am getting an error.I really appreciate any help.Thanks in Advance.

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' (SELECT @rownum := 0) t ORDER BY vote DESC' at line 2: SELECT @rownum := @rownum + 1 AS rank, name, vote FROM uservotes WHERE name= A , (SELECT @rownum := 0) t ORDER BY vote DESC

  • 写回答

3条回答 默认 最新

  • dongzhuang6247 2014-01-01 22:00
    关注

    1) Try this one:

    SELECT 
        @i:=@i+1 AS rn, 
        t.*
    FROM 
        uservotes AS t,
        (SELECT @i:=0) AS tbl
        ORDER BY vote desc
    

    2) Then getting the particular row for A is easy:

    select * From
    (
    SELECT 
        @i:=@i+1 AS rn, 
        t.*
    FROM 
        uservotes AS t,
        (SELECT @i:=0) AS tbl
    ORDER BY vote desc
    
    ) x where x.name = 'A'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi