【问题标题】:Display only the last 300 MYSQL Results with a PHP pagination system使用 PHP 分页系统仅显示最后 300 个 MYSQL 结果
【发布时间】:2013-06-25 18:10:54
【问题描述】:

嗨,我有一个系统,我只想显示来自 MYSQL 的最后 300 条记录,通常我会像这样 LIMIT 300 编写查询

我遇到的问题是我正在使用一个分页系统来编写这样的查询。

    $tableName="masterip_details";      
$targetpage ="raw_data.php";    
$limit = 30; 

$query = "SELECT COUNT(*) as num FROM $tableName where type='6' AND country_code='GB'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
    $start = ($page - 1) * $limit; 
}else{
    $start = 0; 
    }   

// Get page data
$query1 = "SELECT * FROM $tableName where type='6' AND country_code='GB' LIMIT $start, $limit";
$result = mysql_query($query1);

问题是因为它使用限制来计算开始和结束页码我不确定是否可以限制使用分页时返回的行数。

【问题讨论】:

  • 为什么不呢? LIMIT x, y 其中 x = 从哪里开始(第 (10 - 1) 页 * 每页 300 = 2700)和 y = 要显示的数量 (300)
  • 你可能会喜欢这个stackoverflow.com/questions/8060213/…
  • 抱歉可能没有正确理解这个问题......我保留我的第一条评论,因为它可能仍然很有趣。如果你想要最后 300 个条目,LIMIT $total_pages-300,300
  • 嗨陈,我不想每页显示 300 个我想通过只从系统中获取最后 300 条记录来显示每页 30 个结果
  • 使用子选择使用 ORDER BY 和 LIMIT 获取最新的 300 条记录,然后从中选择您想要的页面记录以及另一个 ORDER BY / LIMIT。虽然很乱

标签: php mysql


【解决方案1】:
select * from (SELECT * FROM $tableName where type='6' AND country_code='GB' order by AUTO_INCERMENT_ID DESC LIMIT 300) as a order by AUTO_INCERMENT_ID ASC LIMIT $start, $limit

【讨论】:

  • 谢谢,但这根本没有任何区别
  • 你期待什么不同?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
  • 2015-10-08
  • 2012-05-19
  • 2012-09-05
相关资源
最近更新 更多