不好意思,上面的不对:假设你的最大值是$max,取出你想改变的那个记录的值$temp,那就这样写:update table1 set listid='".$max."' where listid='".$temp."' update table1 set listid=listid-1 where listid>'".$temp."' and listid<($max+1);
这样的话先前就得先select max(listid) from table1 了吧?可否把这一句并到上面两句中? 另外,既然第一句已经把temp改变成了max,那岂不两个listid都等于max了?应该 update table1 set listid='$max+1' where listid='$temp' update table1 set listid=listid-1 where listid>'$temp' and listid<($max+2); 这样吧?
在这之前就是要先获得listid最大的那条记录,至于你怎么求就无所谓了,这个方法是可以实现你的要求,由于我没有现成的表,所以无法实践,你是否已经调试成功了? 本人认为应该这样写: //先将其它的依次减一 update table1 set listid=listid-1 where listid>'".$temp."' and listid<($max+1); //然后再将该记录置为最大 update table1 set listid='".$max."' where listid='".$temp."' 其实$max,$temp值是先要知道的你那样写,岂不是最大值比原来的大了一??你多试试吧,肯定行的,要不你把数据表结构和数据给我一份,我给你调调看看
//然后再将该记录置为最大(确保id 是唯一的) update table1 set listid='".$max."' where id='".$temp."' 这样就可以了,我自己创建了个表,测试通过。
/**获得该公司的相关信息**/ $sql="select max(listid) as max_listid from test ;"; $result1 = $mysql_server->send_result_sql($sql,0); $listrow=mysql_fetch_array($result1); $max_listid=$listrow[max_listid]; echo $max_listid; $temp=2;//先将其它的依次减一 $sql="update test set listid=listid-1 where listid>'".$temp."' and listid<($max_listid+1);"; $result1 = $mysql_server->send_result_sql($sql,0); //然后再将该记录置为最大 $sql="update test set listid='".$max_listid."' where id='".$temp."' "; $result1 = $mysql_server->send_result_sql($sql,0);这是我亲自测试的,没问题,可以实现你说的功能 (数据库连接你们的即可,我是用的类,你看看就明白了) 你自己将它整理成函数,在需要的地方调用即可。
没那么麻烦,前两个回答比较简单,不过,如果记录数不是5个呢? 用两个sql不就实现了嘛!! update table1 set listid=listid-1 //这句是让所有的记录都加1 update table1 set listid=5 where name=b //这句是让b的listid变成5 多简单! 你的记录如果有10000条,这两个语句仍然适用!
telescope(望远镜) :你这样做,并没有实现他的要求,如下:************** id name listid listid(全部减一) listid(b->5) listid(实际想实现) 1 a 1 0 0 1 2 b 2 1 5 5 4 c 3 2 2 2 8 d 4 3 3 3 9 md 5 4 4 4 我的其实也是两条sql语句。
select * from table1 order by listid;
,listid='5' 是什么意思?如何实现了b的值变为最大?
另外,既然第一句已经把temp改变成了max,那岂不两个listid都等于max了?应该
update table1 set listid='$max+1' where listid='$temp'
update table1 set listid=listid-1 where listid>'$temp' and listid<($max+2);
这样吧?
本人认为应该这样写:
//先将其它的依次减一
update table1 set listid=listid-1 where listid>'".$temp."' and listid<($max+1);
//然后再将该记录置为最大
update table1 set listid='".$max."' where listid='".$temp."' 其实$max,$temp值是先要知道的你那样写,岂不是最大值比原来的大了一??你多试试吧,肯定行的,要不你把数据表结构和数据给我一份,我给你调调看看
我那样写没有大一啊。因为第二个语句已经listid=listid-1了嘛。之所以我想在尽可能少的语句里面实现,是为了高效的利用这个自定义的小函数。因为多处用到。而那个最大值除了此函数以外没什么用,所以在该函数里面读出即可。而不是传递过来的。
update table1 set listid='".$max."' where id='".$temp."'
这样就可以了,我自己创建了个表,测试通过。
$sql="select max(listid) as max_listid from test ;";
$result1 = $mysql_server->send_result_sql($sql,0);
$listrow=mysql_fetch_array($result1);
$max_listid=$listrow[max_listid];
echo $max_listid;
$temp=2;//先将其它的依次减一
$sql="update test set listid=listid-1 where listid>'".$temp."' and listid<($max_listid+1);";
$result1 = $mysql_server->send_result_sql($sql,0);
//然后再将该记录置为最大
$sql="update test set listid='".$max_listid."' where id='".$temp."' ";
$result1 = $mysql_server->send_result_sql($sql,0);这是我亲自测试的,没问题,可以实现你说的功能
(数据库连接你们的即可,我是用的类,你看看就明白了)
你自己将它整理成函数,在需要的地方调用即可。
用两个sql不就实现了嘛!!
update table1 set listid=listid-1 //这句是让所有的记录都加1
update table1 set listid=5 where name=b //这句是让b的listid变成5
多简单!
你的记录如果有10000条,这两个语句仍然适用!
id name listid listid(全部减一) listid(b->5) listid(实际想实现)
1 a 1 0 0 1
2 b 2 1 5 5
4 c 3 2 2 2
8 d 4 3 3 3
9 md 5 4 4 4
我的其实也是两条sql语句。