1

I have a table like this:

id  subject        position         visible

1   Home               1               1
2   About us           2               1
3   Our partners       3               1
18  Products           4               1
19  Extra              5               0

I want to add number 1 to all rows after position=2, like this:

id  subject        position         visible

1   Home               1               1
2   About us           2               1
3   Our partners       4               1
18  Products           5               1
19  Extra              6               0

I tried like this, but this doesnt work:

$row=mysql_query("SELECT * FROM table");
$count=mysql_num_rows($row);
$position=3;
$add=($position+1);
while($position<=$count)
{
    $sql="UPDATE subjects SET position=$add WHERE position=$position";
    mysql_query($sql);
    $add++;
    $position++;
}

After applying above code, my table looks like this:

id  subject        position         visible

1   Home               1               1
2   About us           2               1
3   Our partners       6               1
18  Products           6               1
19  Extra              6               0

Is there any solution?

4

3 回答 3

11

Why not just:

UPDATE subjects SET position=position+1 WHERE position>2
于 2012-04-10T19:34:25.053 回答
3
UPDATE subjects SET position=position + 1 WHERE position >= 3
于 2012-04-10T19:35:03.250 回答
1
 update subjects set position = position +1 where position >2
于 2012-04-10T19:37:36.600 回答