对用户和联接表进行排名

时间:2014-02-17 21:21:36

标签: mysql sql

如何根据点对用户进行排名,并将user_sno与另一个表的refno相结合? 我没有使用以下代码获得正确的排名:

select *, (@rank := @rank + 1) as rank
from tblB uv 
 join tblC c on uv.sno=c.refno
 join
     (select @rank := 0) const
where uv.sno in (2, 4,5) 
order by rank;

我非常感谢任何帮助。谢谢你。

http://sqlfiddle.com/#!2/9be59/12

CREATE TABLE if not exists tblB
(
id int(11) NOT NULL auto_increment ,
sno varchar(255),
name varchar(255),
 PRIMARY KEY (id)
);

CREATE TABLE if not exists tblC
(
id int(11) NOT NULL auto_increment ,
data varchar(255),
  refno varchar(255),
  points int(255),
 PRIMARY KEY (id)
);






INSERT INTO tblB (sno, name ) VALUES
('1', 'Aa'),
('2', 'Bb'),
('3', 'Cc'),
('4', 'Dd'),
('5', 'Ee'),
('6', 'Ff'),
('7', 'Gg'),
('8', 'Hh');


INSERT INTO tblC (data,refno,points ) VALUES
('data1', '1', '101'),
('data2', '2', '102'),
('data3', '3', '103'),
('data4', '4', '101'),
('data5', '5', '102'),
('data6', '6', '103'),
('data7', '7', '101'),
('data8', '8', '101'),
('data9', '9', '101');

1 个答案:

答案 0 :(得分:1)

ORDER BY rank,但您希望ORDER BY points,似乎是一个错字。