Php 将分页添加到内部联接表
如何使用特定查询添加分页 我的问题是:Php 将分页添加到内部联接表,php,mysql,paginate,Php,Mysql,Paginate,如何使用特定查询添加分页 我的问题是: SELECT emptb.*, tempstore.* FROM (SELECT * FROM emptb WHERE Department = :dept)emptb inner join tempstore on emptb.EmpID = tempstore.EmpID WHERE tempstore.ValidDate BETWEEN DATE(:fromDate) AND DATE(:toDate) 注意:当我添加分页时,“下一个”和“上一个
SELECT emptb.*, tempstore.* FROM (SELECT * FROM emptb WHERE Department = :dept)emptb inner join tempstore
on emptb.EmpID = tempstore.EmpID WHERE tempstore.ValidDate BETWEEN DATE(:fromDate) AND DATE(:toDate)
注意:当我添加分页时,“下一个”和“上一个”按钮不起作用,它被卡在它在表中看到的第一条记录上。你能给我一些好的分页来处理这种查询吗?分页是有效的,因为你对sql子句指定了一个限制,但在上面的查询中没有限制。另外,我发现您现在需要知道您可以期望的记录总数-因此需要计数 举个简单的例子,希望这就是你的意思
$pageNumber=0;
$startRow=0;
$maxRows=10;
$sql="select
(
select count(*) from ( select * from `emptb` where `department` = :dept ) emptb
inner join `tempstore` on emptb.`empid` = tempstore.`empid`
where tempstore.`validdate`
between date(:fromdate) and date(:todate)
) as 'dbrecordcount',
emptb.*,
tempstore.* from ( select * from `emptb` where `department` = :dept ) emptb
inner join `tempstore` on emptb.`empid` = tempstore.`empid`
where tempstore.`validdate`
between date(:fromdate) and date(:todate)
limit $startRow, $maxRows;";
/* above would show the first 10 records */
-> ie: records 0,10
/* Page number is increased by 1 after clicking pagination link / button */
-> $startRow=$pageNumber * $maxRows ~ equals 10;
-> would then show records from 10,20
/* To derive the pagination links, something like this perhaps... */
/* Number of records per page */
$maxRows=10;
/* The total number of expected rows: uses db result 'dbrecordcount' */
$totalRows=$db->dbrecordcount;
/* Current page within paged results: retrieve `page` from querystring if using GET method */
$pageNumber=$_GET['page'];
/* How many pages of results are there */
$totalPages=( $maxRows > 0 ) ? abs( ceil( $totalRows / $maxRows ) - 1 ) : 0;
/* Where does paging begin */
$startRow=$pageNumber * $maxRows;
/* Display pagination links if there are sufficient number of records */
if( $totalPages > 0 && $totalRows > $maxRows ){
/* First record link */
if( $pageNumber==0 ) {
echo "First";
} else {
echo "<a href='?page=0'>First</a>";
}
/* Previous record link */
if( $pageNumber > 0 ){
echo "<a href='?page=".max( 0, $pageNumber - 1 )."'>Previous</a>";
} else {
echo "Previous";
}
/* Next record link */
if( ( $pageNumber + 1 ) > $totalPages ){
echo 'Next';
} else {
echo "<a href='?page=".min( $totalPages, $pageNumber + 1 )."'>Next</a>";
}
/* Last record link */
if( $pageNumber==$totalPages ){
echo 'Last';
} else {
echo "<a href='?page=".$totalPages."'>Last</a>";
}
}
您必须在查询中使用偏移量和限制。你有没有加入并不重要。请分享你的表格结构和你想要做的事情。我会帮助你的。我想要的结果是每页查看每个员工的记录,即emptb和他们各自的出勤记录,即tempstore。重新措辞,按每个员工的日期显示每个考勤记录。我无法在外部查询中设置限额,因为它将只显示一个日期,但如果我在内部查询中设置lmit,则“下一步”和“上一步”按钮不起作用,但我已成功显示每位员工的出勤情况。不要使用子选择。用连接解决它。请共享该结构,我将帮助您。在employee表中,它包含主键id自动递增、employee id、lastname、firstname、department。考勤表具有id主键自动递增、员工id、日期、考勤、午餐、午餐、超时。我将如何重新构造查询,以符合即使是最分页的代码的要求。我可以用union替换它吗?谢谢你的回复,我会尝试你的解决方案。我还有一个问题,最大行数是否可以灵活地处理1000条记录?是的,您可以将最大行数设置为您想要的任何数字-尽管每页显示1000条记录可能需要一段时间才能呈现,而且没有人会查看每条记录。上面的内容没有经过测试,但当您尝试在页面上进行分页时,应该会给您一个良好的开端。祝你好运假设搜索结果中有1000行,人力资源经理希望每页查看每个员工的记录。我该怎么做??如果总共有1000行,并且他希望一次看到所有行,那么您不需要分页。如果总共有1000多行,他希望看到每页1000行设置$maxRows=1000我忘了问为什么您在外部查询中设置了限制,结果将只显示一个日期记录,比如说每个员工1月份,而不是每个员工1月份的全部记录?