简体   繁体   中英

MySQL timestamp comparison with less than sign

I made a PHP blog. Everything is up and running except one glitch. I want to display the articles in decreasing order sorted by date. So whenever an article is added to the DB an auto timestamp records the time of insertion.

GLITCH - > i want to display 5 articles per page. I did that easily for the first page by using this

$sql="SELECT id,article_name,article_body,date 
         from articles order by date desc limit 5" ;

Now i want to proceed with the second page and would like the second page to display the articles from where the page 1 left. If there are 10 articles in descending order, page 1 should display the first 5 and page 2 should display the next five.

This logic should work realtime when many articles are added per day. i used this query but it's displaying just 1 row.

$sql="select id,article_name,article_body,unix_timestamp(date) 
           from articles 
           where date < (select unix_timestamp(date)
                 from articles order by date desc limit $n,1 ) 
           order by date desc limit $n,5" ;

$n - the id from where the rows will be extracted.

utilize the offset in sql:

$items_per_page=5;   
$offset=($current_page-1)* $items_per_page;

the sql:

SELECT id,article_name,article_body,date 
         from articles order by date desc LIMIT  $items_per_page OFFSET $offset

try:

select id,article_name,article_body,unix_timestamp(date) 
from articles 
where date < ( select date from articles order by date desc limit $n,1 ) 
order by date desc limit $n,5

where $n should be the last id from previous page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM