简体   繁体   English

MySQL时间戳与低于符号的比较

[英]MySQL timestamp comparison with less than sign

I made a PHP blog. 我做了一个PHP博客。 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. GLITCH - >我想每页显示5篇文章。 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. 现在我想继续第二页,并希望第二页显示第1页离开的文章。 If there are 10 articles in descending order, page 1 should display the first 5 and page 2 should display the next five. 如果有10篇文章按降序排列,则第1页应显示前5个,第2页应显示下5个。

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. $ n - 提取行的ID。

utilize the offset in sql: 利用sql中的偏移量:

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

the sql: 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 其中$ n应该是上一页的最后一个id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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