select * from info where 被投票人的数 > (select 被投票人的数 from info where user='123');
select d.*, @r:=if(@c>c,@r+1,@r) as 排名, @c:=c from( select 被投票人, count(*) as c from 表 group by 被投票人 order by c desc )d,(select @c:=null, @r:=1) _;
没有提供案例,不知道这个是不是你要的结果 declare @t table (No int,person varchar(100)) insert into @t select 1,'张三' union ALL select 2,'张三' union ALL select 3,'李四' union ALL select 4,'王五' union ALL select 5,'张三' union ALL select 6,'张三' union ALL select 7,'张三' union ALL select 8,'李四' ---以上测试数据 SELECT * FROM ( select person,count(1) as countNo from @t Group by person ---按人员分组求出每个人员的投票总数量 ) AS A--把里面这段SQL当临时表A WHERE A.countNo>(SELECT COUNT(distinct person)+1 AS No from @t)---在A表中查出数量大于投标人数+1的人员 --如案例中总投票人数为3人,按你的要求查出大于总人数+1的人员 ,所以只有张三满足
from info
where 被投票人的数 > (select 被投票人的数 from info where user='123');
from(
select 被投票人, count(*) as c from 表 group by 被投票人 order by c desc
)d,(select @c:=null, @r:=1) _;
declare @t table (No int,person varchar(100))
insert into @t
select 1,'张三' union ALL
select 2,'张三' union ALL
select 3,'李四' union ALL
select 4,'王五' union ALL
select 5,'张三' union ALL
select 6,'张三' union ALL
select 7,'张三' union ALL
select 8,'李四'
---以上测试数据
SELECT * FROM (
select person,count(1) as countNo
from @t
Group by person ---按人员分组求出每个人员的投票总数量
) AS A--把里面这段SQL当临时表A
WHERE A.countNo>(SELECT COUNT(distinct person)+1 AS No from @t)---在A表中查出数量大于投标人数+1的人员
--如案例中总投票人数为3人,按你的要求查出大于总人数+1的人员 ,所以只有张三满足