开发者

mysql search get specific rows

Suppose i have a variable ($var) that determines what rows get selected for an mysql query.The table to be searched has a date column along with some others.

  • If the value of $var = 1 then retrieve top 5 rows in desc date order.
  • If the value of $var = 2 then retrieve rows 6-10 in desc date order.
  • If the value of $var = 3 then retrieve rows 11-1开发者_如何转开发5 in desc date order.


SELECT
    *
FROM
    my_table
ORDER BY
    my_table.date DESC
LIMIT
   [($var - 1) * 5], 5

Where [] is where you should embed your PHP using . to concatenate strings if $var is between 1 and 3


This is a simple as using the OFFSET syntax in the mysql SELECT statement:

SELECT * FROM myTable ORDER BY date DESC LIMIT ($var*5, ($var-1)*5 +1)


select * from table order by date desc limit (($var-1)*5+1), ($var*5)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜