在下面設置此結果;我試圖將紅色數字插入到post_position中 - 從1開始,每個topic_id
有多少行。MySQL - 爲每個「組」插入增量編號
這可以使用phpMyAdmin中的簡單查詢來完成,還是需要使用PHP? (?如果我需要的PHP,你能不能用簡短的片斷幫助)
在下面設置此結果;我試圖將紅色數字插入到post_position中 - 從1開始,每個topic_id
有多少行。MySQL - 爲每個「組」插入增量編號
這可以使用phpMyAdmin中的簡單查詢來完成,還是需要使用PHP? (?如果我需要的PHP,你能不能用簡短的片斷幫助)
兄弟,我認爲是最糟糕的設計表:(添加
id
每個值;
make php之後:
$result = mysql_query("SELECT * FROM <table> ORDER BY topic_id, id");
$count = 1;
while($r = mysql_fetch_assc($result){
if(!isset($topic_id) || $topic_id != $r['topic_id]){
$topic_id = $r['topic_id];
$count = 1;
}
mysql_query("UPDATE <table> SET post_position = {$count} WHERE `id` = {$r['id'}");
$count++;
}
你可以在SQL層做一些ser ver-side變量和查詢本身中的一堆醜陋邏輯。但在PHP中使用它會容易得多。
select @last_group := null; @row_num := 0;
select if(@last_group <> grouped_field_in_your_table, @row_num := @row_num + 1, @row_num := 1),
other,fields,here
@last_group := group_field_in_your_table
from ....
醜陋,但會有點工作。
這不是整個表格,我只是將其過濾掉。 ;)它每行都有一個唯一的ID。我會給你的代碼一個測試。謝謝!! – lee 2011-05-26 17:12:11