0

I would like to select the first x rows say 10 before an insert. If there are no rows before the insert then it just returns the row that has been inserted

After an insert I am able to get the id by using mysql_insert_id();

Is the an intelligent way to select the first 10 rows before this insert.

This must work for any number within the interval 0 or 10. So suppose they are 7 rows before the insert then it would just return 8 rows.

I was thinking of using limit like so

  $insertid  = mysql_insert_id()
  $postid    = 200
  Select a, b, c from mytable where a = '$postid' limit $insertid 10 order by b

but this doesn't work like I assumed.

4

2 回答 2

1
Select a, b, c from mytable where primary_key_col < '$insertid' and a = 200
ORDER BY primary_key_col DESC LIMIT 10
于 2013-10-30T17:01:51.430 回答
0

您可以使用 BETWEEN :

Select a, b, c from mytable where a BETWEEN  $postid - 10 AND $postid 

(顺序很重要)

另外,你应该使用 mysqli_ 而不是 mysql

于 2013-10-30T17:07:01.867 回答