用这样的语句,把生成结果insert到一个空表里即可 select a.id, a.a2, count(a.id) as ccc from t3 a, t3 b where a.a2>=b.a2 group by a.id order by a.a2测试表及数据:CREATE TABLE `t3` ( `id` int(11) unsigned NOT NULL auto_increment, `a1` int(11) unsigned NOT NULL, `a2` varchar(50) NOT NULL, `a3` varchar(50) NOT NULL, `a4` datetime NOT NULL, `a5` text, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;/*Data for the table `t3` */insert into `t3` (`id`,`a1`,`a2`,`a3`,`a4`,`a5`) values (1,1,'444','1','0000-00-00 00:00:00',NULL),(2,1,'999','1','0000-00-00 00:00:00',NULL),(3,1,'111','1','0000-00-00 00:00:00',NULL),(4,2,'333','1','0000-00-00 00:00:00',NULL),(5,2,'888','1','0000-00-00 00:00:00',NULL),(6,3,'777','1','0000-00-00 00:00:00',NULL),(7,1,'222','1','0000-00-00 00:00:00',NULL),(8,2,'555','1','0000-00-00 00:00:00',NULL),(9,3,'666','1','0000-00-00 00:00:00',NULL);
用sort函数排序
汗死了,你说的是用PHP语言实现。数据库的话用order by语法,
PHP直接提取结果就可以了。
select a.id, a.a2, count(a.id) as ccc
from t3 a, t3 b
where a.a2>=b.a2
group by a.id
order by a.a2测试表及数据:CREATE TABLE `t3` (
`id` int(11) unsigned NOT NULL auto_increment,
`a1` int(11) unsigned NOT NULL,
`a2` varchar(50) NOT NULL,
`a3` varchar(50) NOT NULL,
`a4` datetime NOT NULL,
`a5` text,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;/*Data for the table `t3` */insert into `t3` (`id`,`a1`,`a2`,`a3`,`a4`,`a5`) values (1,1,'444','1','0000-00-00 00:00:00',NULL),(2,1,'999','1','0000-00-00 00:00:00',NULL),(3,1,'111','1','0000-00-00 00:00:00',NULL),(4,2,'333','1','0000-00-00 00:00:00',NULL),(5,2,'888','1','0000-00-00 00:00:00',NULL),(6,3,'777','1','0000-00-00 00:00:00',NULL),(7,1,'222','1','0000-00-00 00:00:00',NULL),(8,2,'555','1','0000-00-00 00:00:00',NULL),(9,3,'666','1','0000-00-00 00:00:00',NULL);
lz要问的是那种情况的排序?回答你得你够写一篇论文了。
1.大多数web系统的实际运用中,从数据库提取数据的问题
从PHP程序员的角度,就是order by的问题。具体再怎么高效,那就是数据库管理的问题了,超出PHP的范围。
2.从数据库提取无序数据之后,进行排序
是给学生讲的排序算法问题吗?还是要用PHP写个RDB的功能?数据量大了,想要高效处理,哪一个深究起来都够写点内容了。
lz的提问可真不简单呐。
for就够了,做了一个从多少条到多少条处理,然后跳转到下一个多少条到多少条,这样脚本不会超时。
反正是整理数据库,又不要考虑多高的效率
for($i=1;$i<=$total;$i++){
$sql=" update .... where id='$i'";
mysql_query($sql);
}
这样也可以实现的
create table tb1(
id int(10) auto_increment primary key,
paixu int(10) not null default 0,
other_value text
);
insert into tb1(id,paixu,other_value) values (3,0,'aaa');
insert into tb1(id,paixu,other_value) values (51,0,'bbb');
insert into tb1(id,paixu,other_value) values (225,0,'ccc');
insert into tb1(id,paixu,other_value) values (12,0,'aaa');
insert into tb1(id,paixu,other_value) values (21,0,'bbb');
insert into tb1(id,paixu,other_value) values (85,0,'ccc');
create table tb2(
id int(10) auto_increment primary key,
tb1_id int(10) not null default 0,
paixu int(10) not null default 0,
other_value text
);
insert into tb2 (tb1_id,paixu,other_value) select * from tb1;
update tb2 set paixu=id;
alter table tb2 drop column id;
alter table tb2 change tb1_id id int(10) not null primary key;
drop table tb1;
rename table tb2 to tb1;
mysql> select * from tb1;
+-----+-------+-------------+
| id | paixu | other_value |
+-----+-------+-------------+
| 3 | 1 | aaa |
| 51 | 2 | bbb |
| 225 | 3 | ccc |
| 12 | 4 | aaa |
| 21 | 5 | bbb |
| 85 | 6 | ccc |
+-----+-------+-------------+
6 rows in set (0.00 sec)
也不知道大量记录,如上百万甚至上千万条的情况下速度如何。
例如
$s_i=0;
再
$s_i++;就行
mySQL数据库,一个表,想把某字段的值按照顺序更新为1,2,3,4,5,6,7更新的时候也要先有个顺序排列吧,是主键递增的顺序更新,还是主键递减更新?