在下面设置此结果;我试图将红色数字插入到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