慕尼黑8549860
2023-03-05 19:19:50
我的目的是修改字段b的值為數(shù)字序號,第一行是1,第二行是2,第n行是n,一直修改完所有記錄,這條語句怎么寫?
2 回答

翻翻過去那場雪
TA貢獻(xiàn)2065條經(jīng)驗 獲得超14個贊
如果表a有自增長ID的話就很好辦了 update a set b = id 如果沒的話寫一個PHP的小程序就好
<?php
$con = mysql_connect("localhost","root","password") or die("數(shù)據(jù)庫連接失敗".mysql_error());//連接數(shù)據(jù)庫
mysql_select_db("test",$con) or die ("數(shù)據(jù)庫選擇失敗".mysql_error());
mysql_query("set names gbk");//設(shè)置編碼格式
$a = mysql_query("select * from a");
$n = 1;
while($b = mysql_fetch_array($a))
{
mysql_query("update a set b = "."'".$n."'"." where id = ".$b['id']); //where 后面的條件語句可以是任意該表內(nèi)字段
$n++;
}

翻閱古今
TA貢獻(xiàn)1780條經(jīng)驗 獲得超5個贊
將update語句放到for循環(huán)中
如果是在數(shù)據(jù)庫上操作,可以用T-SQL處理: eg: 表:ttt (id )有7條記錄, id: 1 2 3 4 5 6 7 declare @i int set @i=7 while @i>0 begin --insert into ttt values(@i) update ttt set id=@i+1 where id=@i print @i set @i=@i-1 end go 一定要倒序循環(huán) 結(jié)果: id: 2 3 4 5 6 7 8 這樣寫:update ttt set id = id +1 where id in (select id from ttt) 也行 程序中在寫sql 的地方 可以套用for循環(huán)
添加回答
舉報
0/150
提交
取消