排名sql
2010-11-09 19:55
169 查看
mysql> use test2;
Database changed
mysql> create table scores
-> (
-> user int not null auto_increment primary key
-> ,
-> scores int not null);
Query OK, 0 rows affected (0.16 sec)
mysql> insert into scores(scores)
-> values(80),(90),(7),(99),(100);
Query OK, 5 rows affected (0.09 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from scores;
+------+--------+
| user | scores |
+------+--------+
| 1 | 80 |
| 2 | 90 |
| 3 | 7 |
| 4 | 99 |
| 5 | 100 |
+------+--------+
5 rows in set (0.00 sec)
mysql> select user,scores,(select count(*) from scores where scores >=(select sc
ores from scores where user=4 order by scores desc limit 1)) as pm from scores w
here user=4;
+------+--------+------+
| user | scores | pm |
+------+--------+------+
| 4 | 99 | 2 |
+------+--------+------+
1 row in set (0.00 sec)
function getTOP(&$db_class,$type='total',$user_id=NULL){
$result = array();
if($type=='total' && $user_id!==NULL){
$sql ='SELECT user_id,total, ( SELECT count( * ) FROM top WHERE total >= ( SELECT total FROM top WHERE user_id ='.$user_id.' ORDER BY total DESC LIMIT 1 ) ) AS pm FROM top WHERE user_id ='.$user_id;
$rs = $db_class->query($sql);
while($row = $db_class->fetchArray($rs)){
if($row) array_push($result,$row);
}
}else(($type=='type1' || $type=='type2' || $type=='type3') && $user_id!==NULL){
$sql = 'SELECT user_id,'.$type.', (
SELECT count( * )
FROM top
WHERE '.$type.' >= (
SELECT '.$type.'
FROM top
WHERE user_id ='.$user_id.' ORDER BY '.$type.' DESC
LIMIT 1 )
) AS pm
FROM top
WHERE user_id ='.$user_id;
$rs = $db_class->query($sql);
$result = $db_class->fetchArray($rs)
}
return $result;
}
//total
replace into top (select p.user_id,count(v.pic_id),0,0,0 from tbl_vote v,tbl_pic p where v.pic_id=p.id group by v.pic_id)
//type1
replace into top (select p.user_id,t.total,count(v.pic_id),0,0 from tbl_vote v,tbl_pic p,top t where t.user_id=p.user_id and v.pic_id=p.id and v.type=1 group by v.pic_id)
//type2
replace into top (select p.user_id,t.total,t.type1,count(v.pic_id),0 from tbl_vote v,tbl_pic p,top t where t.user_id=p.user_id and v.pic_id=p.id and v.type=2 group by v.pic_id)
//type3
replace into top (select p.user_id,t.total,t.type1,t.type2,count(v.pic_id) from tbl_vote v,tbl_pic p,top t where t.user_id=p.user_id and v.pic_id=p.id and v.type=3 group by v.pic_id)
//total 排名
SELECT user_id, total, (
SELECT count( * )
FROM top
WHERE total >= (
SELECT total
FROM top
WHERE user_id =100011
ORDER BY total DESC
LIMIT 1 )
) AS pm
FROM top
WHERE user_id =100011;
//type1 排名
SELECT user_id, type1, (
SELECT count( * )
FROM top
WHERE type1 >= (
SELECT type1
FROM top
WHERE user_id =100011
ORDER BY type1 DESC
LIMIT 1 )
) AS pm
FROM top
WHERE user_id =100011;
//type2排名
SELECT user_id, type2, (
SELECT count( * )
FROM top
WHERE type2 >= (
SELECT type2
FROM top
WHERE user_id =100011
ORDER BY type2 DESC
LIMIT 1 )
) AS pm
FROM top
WHERE user_id =100011;
//type3排名
SELECT user_id, type3, (
SELECT count( * )
FROM top
WHERE type3 >= (
SELECT type3
FROM top
WHERE user_id =100011
ORDER BY type3 DESC
LIMIT 1 )
) AS pm
FROM top
WHERE user_id =100011;
-------------------------------------------------------------------------------------------------------------------
select (select count(*) from `top` where k.total<total or (k.total=total and k.user_id>=user_id)) as pm
from `top` k
where user_id=100011;
Database changed
mysql> create table scores
-> (
-> user int not null auto_increment primary key
-> ,
-> scores int not null);
Query OK, 0 rows affected (0.16 sec)
mysql> insert into scores(scores)
-> values(80),(90),(7),(99),(100);
Query OK, 5 rows affected (0.09 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from scores;
+------+--------+
| user | scores |
+------+--------+
| 1 | 80 |
| 2 | 90 |
| 3 | 7 |
| 4 | 99 |
| 5 | 100 |
+------+--------+
5 rows in set (0.00 sec)
mysql> select user,scores,(select count(*) from scores where scores >=(select sc
ores from scores where user=4 order by scores desc limit 1)) as pm from scores w
here user=4;
+------+--------+------+
| user | scores | pm |
+------+--------+------+
| 4 | 99 | 2 |
+------+--------+------+
1 row in set (0.00 sec)
function getTOP(&$db_class,$type='total',$user_id=NULL){
$result = array();
if($type=='total' && $user_id!==NULL){
$sql ='SELECT user_id,total, ( SELECT count( * ) FROM top WHERE total >= ( SELECT total FROM top WHERE user_id ='.$user_id.' ORDER BY total DESC LIMIT 1 ) ) AS pm FROM top WHERE user_id ='.$user_id;
$rs = $db_class->query($sql);
while($row = $db_class->fetchArray($rs)){
if($row) array_push($result,$row);
}
}else(($type=='type1' || $type=='type2' || $type=='type3') && $user_id!==NULL){
$sql = 'SELECT user_id,'.$type.', (
SELECT count( * )
FROM top
WHERE '.$type.' >= (
SELECT '.$type.'
FROM top
WHERE user_id ='.$user_id.' ORDER BY '.$type.' DESC
LIMIT 1 )
) AS pm
FROM top
WHERE user_id ='.$user_id;
$rs = $db_class->query($sql);
$result = $db_class->fetchArray($rs)
}
return $result;
}
//total
replace into top (select p.user_id,count(v.pic_id),0,0,0 from tbl_vote v,tbl_pic p where v.pic_id=p.id group by v.pic_id)
//type1
replace into top (select p.user_id,t.total,count(v.pic_id),0,0 from tbl_vote v,tbl_pic p,top t where t.user_id=p.user_id and v.pic_id=p.id and v.type=1 group by v.pic_id)
//type2
replace into top (select p.user_id,t.total,t.type1,count(v.pic_id),0 from tbl_vote v,tbl_pic p,top t where t.user_id=p.user_id and v.pic_id=p.id and v.type=2 group by v.pic_id)
//type3
replace into top (select p.user_id,t.total,t.type1,t.type2,count(v.pic_id) from tbl_vote v,tbl_pic p,top t where t.user_id=p.user_id and v.pic_id=p.id and v.type=3 group by v.pic_id)
//total 排名
SELECT user_id, total, (
SELECT count( * )
FROM top
WHERE total >= (
SELECT total
FROM top
WHERE user_id =100011
ORDER BY total DESC
LIMIT 1 )
) AS pm
FROM top
WHERE user_id =100011;
//type1 排名
SELECT user_id, type1, (
SELECT count( * )
FROM top
WHERE type1 >= (
SELECT type1
FROM top
WHERE user_id =100011
ORDER BY type1 DESC
LIMIT 1 )
) AS pm
FROM top
WHERE user_id =100011;
//type2排名
SELECT user_id, type2, (
SELECT count( * )
FROM top
WHERE type2 >= (
SELECT type2
FROM top
WHERE user_id =100011
ORDER BY type2 DESC
LIMIT 1 )
) AS pm
FROM top
WHERE user_id =100011;
//type3排名
SELECT user_id, type3, (
SELECT count( * )
FROM top
WHERE type3 >= (
SELECT type3
FROM top
WHERE user_id =100011
ORDER BY type3 DESC
LIMIT 1 )
) AS pm
FROM top
WHERE user_id =100011;
-------------------------------------------------------------------------------------------------------------------
select (select count(*) from `top` where k.total<total or (k.total=total and k.user_id>=user_id)) as pm
from `top` k
where user_id=100011;
相关文章推荐
- sql 建表以及查询---复杂查询之成绩排名
- 第97课: 使用Spark Streaming+Spark SQL实现在线动态计算出特定时间窗口下的不同种类商品中的热门商品排名
- 成绩排名SQL语句
- 获取星星排行,sql排序,自排名
- Sql Server2005 Transact-SQL 排名函数
- 一道简单的SQL面试题:查询成绩排名第10到第20的学生
- SQL语句实现排名
- 排名SQL
- SQL 语句技巧--排名函数的使用实例
- Sql Server2005 Transact-SQL 新兵器学习总结之-排名函数
- 按总数排名的SQL
- 锋利的SQL-基于窗口的排名计算
- Sql按姓名查出某个人的成绩排名
- MS SQL Server:排名函数详解
- 开发整理-sequoiaDB Java实现统计排名sql
- 数据库排名sql,group by 分组查询按照时间最大值
- SQL2005四个排名函数(row_number、rank、dense_rank和ntile)的比较
- SQL 排名函数
- sql 分组后排名,ROW_NUMBER()OVER()函数的使用
- 两条关于生成排名的SQL语句