我需要選擇什麼設置來在刪除某個值時維護有序隊列。我創建帶有自動增量和主鍵的id表。當我第一次插入數值的時候沒問題。像1,2,3,4,5...
刪除一些值時,訂單更改如1,5,3...
。腳本運行不正常。Mysql配置/設置
一些代碼:
if(isset($submit)){
if($pav2 == ""){
header('Location: index.php');
}
else {
$select = mysql_query("SELECT new_id from naujiena ORDER by new_id");
$zym = mysql_num_rows($select);
if($zym == 0) {
$query = mysql_query("INSERT INTO naujiena (new_id,new_pav) VALUES ('1','$pav2')");
header('Location: index.php');
}
else {
$select2 = mysql_query("SELECT new_id from naujiena ORDER BY new_id DESC LIMIT 1");
$max_public_id = mysql_fetch_row($select2);
$query2 = mysql_query("INSERT INTO naujiena (new_id,new_pav) VALUES ('$max_public_id[0]'+1,'$pav2')");
header('Location: index.php');
}
}
}
當new_pav和NEW_ID剛纔說: 例子:
new_id new_pav
5 some_value
4 some_value
3 some_value
2 some_value
1 some_value
當我刪除的東西就變成: 例如:
new_id new_pav
4 some_value
2 some_value
3 some_value
1 some_value
請添加表格結構,示例數據,預期輸出和恰好卡住的位置。 – 2013-03-10 11:59:45
嘗試使用ROW_NUMBER()函數而不是id請參閱堆棧:http://stackoverflow.com/questions/1895110/row-number-in-mysql – shola 2013-03-10 12:34:45
仍然需要幫助;/ – user2149578 2013-03-10 12:45:25