这个问题的确有难度,
1、需要产生递增的编号用来表示名次
2、相同的数值要有相同的名次
3、本来一个select * from table order by a desc取出后用程序循环产生名次即可。但楼主要求使用mysql语句完成
MYSQL:先建表、插入数据
CREATE TABLE test4 (
id tinyint(4) NOT NULL auto_increment,
a tinyint(4) NOT NULL default '0',
UNIQUE KEY id (id)
) TYPE=MyISAM;
INSERT INTO test4 VALUES (1, 2);
INSERT INTO test4 VALUES (2, 6);
INSERT INTO test4 VALUES (3, 4);
INSERT INTO test4 VALUES (4, 9);
INSERT INTO test4 VALUES (5, 5);
INSERT INTO test4 VALUES (6, 0);
INSERT INTO test4 VALUES (7, 6);
INSERT INTO test4 VALUES (8, 5);
INSERT INTO test4 VALUES (9, 0);
DROP TABLE testtemp; # 删除临时表
CREATE TABLE testtemp ( # 创建临时表
id tinyint(4) NOT NULL auto_increment,
a tinyint(4) NOT NULL default '0',
UNIQUE KEY id (id)
) TYPE=MyISAM;
insert into testtemp select DISTINCT '',a from test4 order by a desc; # 向临时表插入不重复数据数据,利用自增字段产生编号
select b.id as level,a.id,a.a from test4 a,testtemp b where a.a=b.a; # 连接两表产生输出
PHP:
<?php
$sql = array(
"DROP TABLE testtemp",
"CREATE TABLE testtemp (
id tinyint(4) NOT NULL auto_increment,
a tinyint(4) NOT NULL default '0',
UNIQUE KEY id (id)
) TYPE=MyISAM;
",
"insert into testtemp select DISTINCT '',a from test4 order by a desc",
"select b.id as level,a.id,a.a from test4 a,testtemp b where a.a=b.a"
);
$conn = mysql_connect();
mysql_select_db("test");
foreach($sql as $v)
$rs = mysql_query($v);
mysql_result_all($rs);