新建表,并添加数据如下:
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50621
Source Host : localhost:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50621
File Encoding : 65001
Date: 2016-04-23 11:46:02
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `score`
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`score` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES ('1', '少华90', '90');
INSERT INTO `score` VALUES ('2', 'shaohua80', '80');
INSERT INTO `score` VALUES ('3', 'monlone98', '98');
INSERT INTO `score` VALUES ('4', 'lee99', '98');
/*方法一*/
SELECT B.NAME, B.SCORE,
(SELECT COUNT(id) + 1
FROM score A
WHERE A.SCORE > B.SCORE) PM
FROM score B
ORDER BY PM ;
/*方法二,其实也是方法一,只是变成了变量*/
SELECT id, score, rank
FROM (SELECT tmp.id, tmp.score, @rank := @rank + 1 AS rank
FROM (SELECT id, score
FROM score
ORDER BY score desc
) tmp,
(SELECT @rank := 0) s
) RESULT;
分类: web
标签:
搜索
标签
study
ab
amap
apache
apahe
awk
aws
bat
centos
CFS
chrome
cmd
cnpm
composer
consul
crontab
css
curl
cygwin
devops
di
docker
docker,docker-compose
ethereum
excel
fiddler
fluentd
framework
front-end
git
gitgui
github
glide
go
golang
gorm
grafana
gzip
ioc
item2
iterm2
javascript
jenkins
jsonp
kafka
laradock
laravel
larval
linux
liunux
log
mac
mac, wi-fi
macos
magento
mariaDB
minikube
mongoDB
msp
mysql
netbeans
nginx
nodejs
nohup
npm
nsq
php
php-fpm
php7
phpstorm
php扩展
Protobuf
python
redis
scp
server
shell
soap
socket
socket5
sql
sre
ssdb
ssh
ssl
study
sublime
swift
system
td-agent
uml
v2ray
vagrant
vagrnat
vim
vpn
vue
vue.js
webpack
webrtc
websocket
webtatic
windows
windows7
word
wps
xdebug
yarn
yii2
yum
zookeeper
世界国家
互联网
以太坊
分类
前端
小程序
打印机
排序算法
搞笑
权限
粤语
缓存
网络
虚拟机
视频
设计模式
项目管理
热门文章
友情链接