Chinaunix首页 | 论坛 | 博客
  • 博客访问: 312170
  • 博文数量: 27
  • 博客积分: 758
  • 博客等级: 军士长
  • 技术积分: 369
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 23:10
文章分类

全部博文(27)

文章存档

2014年(1)

2012年(26)

我的朋友

分类: Mysql/postgreSQL

2012-05-09 15:35:43

给查询结果增加伪列(感觉不够稳定):
  1. set @i=0; select * from (select @i:=@i+1 as rank, id from player_info) as rank_player_info

给查询结果增加伪列(感觉稳定):
  1. select gold, (@i:=@i+1)as rank from player_info ,(select @i:=0) as rank_table

查询rank:
  1. select id,gold,(select count(*) from player_info where gold>= (select gold
  2. from player_info where id = 100022 order by gold desc limit 1)) as rank from player_info where id = 100022;


大于某个值的若干记录
  1. select id, gold from player_info where gold>= (select gold
  2. from player_info where id = 100013) order by gold desc limit 9

排行榜中位于某个值前面若干位的若干记录

  1. select * from (select id, gold from player_info where gold>= (select gold
  2.     from player_info where id = 100013) order by gold limit 9) as t order by gold desc

排行榜中位于某个值前面若干位的若干记录,附加rank信息
  1. SELECT *
  2. FROM (
  3. SELECT gold, id, (
  4. @i := @i -1
  5. ) AS rank
  6. FROM (
  7. SELECT id, gold
  8. FROM player_info
  9. WHERE gold >= (
  10. SELECT gold
  11. FROM player_info
  12. WHERE id =100013 )
  13. LIMIT 10
  14. ) AS a, (
  15. SELECT @i := (
  16. SELECT count( * )
  17. FROM player_info
  18. WHERE gold >= (
  19. SELECT gold
  20. FROM player_info
  21. WHERE id =100013 ) ) AS base
  22. ) AS b
  23. ) AS c
  24. ORDER BY rank



排行榜TOP10
  1. select id,gold,(@i:=@i 1)as rank from (select id, gold from player_info order by gold desc limit 10)as a ,(select @i:=0) as rank_table

更新rank字段

  1. UPDATE player_info a,
  2. (
  3.  SELECT magic_code,gold,gold_rank, @var:=@var+1 AS PLACE
  4.  FROM player_info,(select @var:=0) as rank_table
  5.  ORDER BY gold desc
  6. ) b
  7. SET a.gold_rank=b.PLACE
  8. WHERE a.magic_code = b.magic_code;


阅读(1788) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

坏坏小丸子2012-05-12 16:40:55

恩,不错的文章,总结的很好啊~~博主

布毫铯2012-05-10 21:34:13

许多都很经典,实战出真知。呵呵