使用分页时计算 运行 total/balance 的最有效方法是什么 (PHP, MySQL)
What's the most efficient way to calculate a running total/balance when using pagination (PHP, MySQL)
我有一个 MySQL table 存储员工记录的里程记录。我有一个 PHP 页面,它以 table(从新到旧)的形式输出每位员工的里程记录以及 运行 余额。这一切都很好。
里程记录的增长速度比预期的要快,现在很明显需要分页。我已经对分页进行了编程,并且效果很好。我现在遇到的问题是分页具有 'broken' 和 运行 平衡,它忽略了我选择的记录之外的任何值。例如,假设您的银行账户只提供了 7 月份的余额,而没有考虑您账户中 6 月份的余额。
关于如何从理论上解决这个问题的建议(我不需要逐行代码)将不胜感激,因为 Google 并没有提供太多帮助。
编辑 1
Database and data
-- phpMyAdmin SQL Dump
-- version 4.0.10.14
-- http://www.phpmyadmin.net
--
-- Host: localhost:3306
-- Generation Time: Jul 21, 2016 at 07:11 PM
-- Server version: 5.1.73-cll
-- PHP Version: 5.4.31
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `propsyst_main`
--
-- --------------------------------------------------------
--
-- Table structure for table `employee_mileage`
--
CREATE TABLE IF NOT EXISTS `employee_mileage` (
`employee_mileage_id` int(11) NOT NULL AUTO_INCREMENT,
`employee_mileage_employee_id` int(11) DEFAULT NULL,
`employee_mileage_vehicle_id` smallint(6) DEFAULT NULL,
`employee_mileage_start_postcode` varchar(8) COLLATE utf8_bin DEFAULT NULL,
`employee_mileage_end_postcode` varchar(8) COLLATE utf8_bin DEFAULT NULL,
`employee_mileage_mileage` decimal(6,2) DEFAULT NULL,
PRIMARY KEY (`employee_mileage_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=8 ;
--
-- Dumping data for table `employee_mileage`
--
INSERT INTO `employee_mileage` (`employee_mileage_id`, `employee_mileage_employee_id`, `employee_mileage_vehicle_id`, `employee_mileage_start_postcode`, `employee_mileage_end_postcode`, `employee_mileage_mileage`) VALUES
(1, 1, 2, 'L17 0BZ', 'L36 9TJ', '1.00'),
(2, 1, 2, 'L17 0BZ', 'L36 9TJ', '2.00'),
(3, 1, 2, 'L17 0BZ', 'L36 9TJ', '3.00'),
(4, 1, 2, 'L17 0BZ', 'L36 9TJ', '4.00'),
(5, 1, 2, 'L17 0BZ', 'L36 9TJ', '5.00'),
(6, 1, 2, 'L17 0BZ', 'L36 9TJ', '6.00'),
(7, 1, 2, 'L17 0BZ', 'L36 9TJ', '7.00');
编辑 2
无效的查询尝试;
$statement = "SELECT *
from (
SELECT em.*, e.*,
@balance := @balance + em.employee_mileage_mileage as balance
FROM employee_mileage em
CROSS JOIN (select @balance := 0) init
INNER JOIN employee e
on em.employee_mileage_employee_id = e.employee_id
where em.employee_mileage_employee_id = " . $employee_id . "
order by em.employee_mileage_id
) as base
ORDER BY em.employee_mileage_id DESC";
$employee_mileage_query = mysqli_query($con,"{$statement} LIMIT {$startpoint} , {$per_page}") or die(mysql_error());
编辑 3
数据库;
-- phpMyAdmin SQL Dump
-- version 4.0.10.14
-- http://www.phpmyadmin.net
--
-- Host: localhost:3306
-- Generation Time: Jul 25, 2016 at 10:22 PM
-- Server version: 5.1.73-cll
-- PHP Version: 5.4.31
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `propsyst_main`
--
-- --------------------------------------------------------
--
-- Table structure for table `employee_mileage`
--
CREATE TABLE IF NOT EXISTS `employee_mileage` (
`employee_mileage_id` int(11) NOT NULL AUTO_INCREMENT,
`employee_mileage_employee_id` int(11) DEFAULT NULL,
`employee_mileage_vehicle_id` smallint(6) DEFAULT NULL,
`employee_mileage_journey_date` date DEFAULT NULL,
`employee_mileage_start_postcode` varchar(8) COLLATE utf8_bin DEFAULT NULL,
`employee_mileage_end_postcode` varchar(8) COLLATE utf8_bin DEFAULT NULL,
`employee_mileage_mileage` decimal(6,2) DEFAULT NULL,
`employee_mileage_date_created` datetime DEFAULT NULL,
`employee_mileage_created_by` int(11) DEFAULT NULL,
PRIMARY KEY (`employee_mileage_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=7 ;
--
-- Dumping data for table `employee_mileage`
--
INSERT INTO `employee_mileage` (`employee_mileage_id`, `employee_mileage_employee_id`, `employee_mileage_vehicle_id`, `employee_mileage_journey_date`, `employee_mileage_start_postcode`, `employee_mileage_end_postcode`, `employee_mileage_mileage`, `employee_mileage_date_created`, `employee_mileage_created_by`) VALUES
(1, 32, 1, '2016-07-15', 'L17 0BZ', 'L6 5BJ', '4.19', '2016-07-25 10:15:01', 32),
(2, 32, 1, '2016-07-15', 'L6 5BJ', 'L17 0BZ', '4.19', '2016-07-25 10:15:01', 32),
(5, 32, 1, '2016-07-23', 'L17 0BZ', 'L17 1AE', '1.55', '2016-07-25 12:14:15', 32),
(3, 32, 1, '2016-07-21', 'L17 0BZ', 'L19 0PD', '2.03', '2016-07-25 12:09:24', 32),
(4, 32, 1, '2016-07-21', 'L19 0PD', 'L17 0BZ', '2.03', '2016-07-25 12:09:24', 32),
(6, 32, 1, '2016-07-23', 'L17 1AE', 'L17 0BZ', '1.55', '2016-07-25 12:14:15', 32);
查询;
$statement = "SELECT *
from (
SELECT em.*, e.*, v.*,
@balance := @balance + em.employee_mileage_mileage as balance
FROM employee_mileage em
CROSS JOIN (select @balance := 0) init
INNER JOIN employee e
on em.employee_mileage_employee_id = e.employee_id
INNER JOIN vehicle v
on em.employee_mileage_vehicle_id = v.vehicle_id
WHERE em.employee_mileage_employee_id = " . $employee_id . "
ORDER BY em.employee_mileage_id
) as base
ORDER BY base.employee_mileage_id DESC";
$employee_mileage_query = mysqli_query($con,"{$statement} LIMIT {$startpoint} , {$per_page}") or die(mysql_error());
输出;
我建议在您的 SQL 查询中计算 运行 余额。
下面是 SQL 语句的示例:
SELECT *
from (
SELECT *,
@balance := @balance + em.employee_mileage_mileage as balance
FROM (select em.*, e.*, v.*
from employee_mileage em
INNER JOIN employee e
on em.employee_mileage_employee_id = e.employee_id
INNER JOIN vehicle v
on em.employee_mileage_vehicle_id = v.vehicle_id
WHERE em.employee_mileage_employee_id = ?
ORDER BY em.employee_mileage_id ASC
) em
CROSS JOIN (select @balance := 0) init
) as base
ORDER BY base.employee_mileage_id DESC
LIMIT ?, 2
上面语句中的参数(标有?
)应该绑定到:
- 您要显示employee_id的数据
- 当前页的起始行(从零开始)
内部查询遍历该特定员工的所有行,并将 运行 余额添加到每条记录。然后外部查询反转排序顺序并将限制应用于它以进行分页。
这个解决方案不需要在 PHP 中进行计算; 运行 余额在查询结果集中很容易获得,并且在每一页上都是正确的。
这是一个小的 SQL fiddle 演示,用于从总共 6 行中获取第二页,每页 2 行,包括正确的余额。
我有一个 MySQL table 存储员工记录的里程记录。我有一个 PHP 页面,它以 table(从新到旧)的形式输出每位员工的里程记录以及 运行 余额。这一切都很好。
里程记录的增长速度比预期的要快,现在很明显需要分页。我已经对分页进行了编程,并且效果很好。我现在遇到的问题是分页具有 'broken' 和 运行 平衡,它忽略了我选择的记录之外的任何值。例如,假设您的银行账户只提供了 7 月份的余额,而没有考虑您账户中 6 月份的余额。
关于如何从理论上解决这个问题的建议(我不需要逐行代码)将不胜感激,因为 Google 并没有提供太多帮助。
编辑 1
Database and data
-- phpMyAdmin SQL Dump
-- version 4.0.10.14
-- http://www.phpmyadmin.net
--
-- Host: localhost:3306
-- Generation Time: Jul 21, 2016 at 07:11 PM
-- Server version: 5.1.73-cll
-- PHP Version: 5.4.31
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `propsyst_main`
--
-- --------------------------------------------------------
--
-- Table structure for table `employee_mileage`
--
CREATE TABLE IF NOT EXISTS `employee_mileage` (
`employee_mileage_id` int(11) NOT NULL AUTO_INCREMENT,
`employee_mileage_employee_id` int(11) DEFAULT NULL,
`employee_mileage_vehicle_id` smallint(6) DEFAULT NULL,
`employee_mileage_start_postcode` varchar(8) COLLATE utf8_bin DEFAULT NULL,
`employee_mileage_end_postcode` varchar(8) COLLATE utf8_bin DEFAULT NULL,
`employee_mileage_mileage` decimal(6,2) DEFAULT NULL,
PRIMARY KEY (`employee_mileage_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=8 ;
--
-- Dumping data for table `employee_mileage`
--
INSERT INTO `employee_mileage` (`employee_mileage_id`, `employee_mileage_employee_id`, `employee_mileage_vehicle_id`, `employee_mileage_start_postcode`, `employee_mileage_end_postcode`, `employee_mileage_mileage`) VALUES
(1, 1, 2, 'L17 0BZ', 'L36 9TJ', '1.00'),
(2, 1, 2, 'L17 0BZ', 'L36 9TJ', '2.00'),
(3, 1, 2, 'L17 0BZ', 'L36 9TJ', '3.00'),
(4, 1, 2, 'L17 0BZ', 'L36 9TJ', '4.00'),
(5, 1, 2, 'L17 0BZ', 'L36 9TJ', '5.00'),
(6, 1, 2, 'L17 0BZ', 'L36 9TJ', '6.00'),
(7, 1, 2, 'L17 0BZ', 'L36 9TJ', '7.00');
编辑 2
无效的查询尝试;
$statement = "SELECT *
from (
SELECT em.*, e.*,
@balance := @balance + em.employee_mileage_mileage as balance
FROM employee_mileage em
CROSS JOIN (select @balance := 0) init
INNER JOIN employee e
on em.employee_mileage_employee_id = e.employee_id
where em.employee_mileage_employee_id = " . $employee_id . "
order by em.employee_mileage_id
) as base
ORDER BY em.employee_mileage_id DESC";
$employee_mileage_query = mysqli_query($con,"{$statement} LIMIT {$startpoint} , {$per_page}") or die(mysql_error());
编辑 3
数据库;
-- phpMyAdmin SQL Dump
-- version 4.0.10.14
-- http://www.phpmyadmin.net
--
-- Host: localhost:3306
-- Generation Time: Jul 25, 2016 at 10:22 PM
-- Server version: 5.1.73-cll
-- PHP Version: 5.4.31
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `propsyst_main`
--
-- --------------------------------------------------------
--
-- Table structure for table `employee_mileage`
--
CREATE TABLE IF NOT EXISTS `employee_mileage` (
`employee_mileage_id` int(11) NOT NULL AUTO_INCREMENT,
`employee_mileage_employee_id` int(11) DEFAULT NULL,
`employee_mileage_vehicle_id` smallint(6) DEFAULT NULL,
`employee_mileage_journey_date` date DEFAULT NULL,
`employee_mileage_start_postcode` varchar(8) COLLATE utf8_bin DEFAULT NULL,
`employee_mileage_end_postcode` varchar(8) COLLATE utf8_bin DEFAULT NULL,
`employee_mileage_mileage` decimal(6,2) DEFAULT NULL,
`employee_mileage_date_created` datetime DEFAULT NULL,
`employee_mileage_created_by` int(11) DEFAULT NULL,
PRIMARY KEY (`employee_mileage_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=7 ;
--
-- Dumping data for table `employee_mileage`
--
INSERT INTO `employee_mileage` (`employee_mileage_id`, `employee_mileage_employee_id`, `employee_mileage_vehicle_id`, `employee_mileage_journey_date`, `employee_mileage_start_postcode`, `employee_mileage_end_postcode`, `employee_mileage_mileage`, `employee_mileage_date_created`, `employee_mileage_created_by`) VALUES
(1, 32, 1, '2016-07-15', 'L17 0BZ', 'L6 5BJ', '4.19', '2016-07-25 10:15:01', 32),
(2, 32, 1, '2016-07-15', 'L6 5BJ', 'L17 0BZ', '4.19', '2016-07-25 10:15:01', 32),
(5, 32, 1, '2016-07-23', 'L17 0BZ', 'L17 1AE', '1.55', '2016-07-25 12:14:15', 32),
(3, 32, 1, '2016-07-21', 'L17 0BZ', 'L19 0PD', '2.03', '2016-07-25 12:09:24', 32),
(4, 32, 1, '2016-07-21', 'L19 0PD', 'L17 0BZ', '2.03', '2016-07-25 12:09:24', 32),
(6, 32, 1, '2016-07-23', 'L17 1AE', 'L17 0BZ', '1.55', '2016-07-25 12:14:15', 32);
查询;
$statement = "SELECT *
from (
SELECT em.*, e.*, v.*,
@balance := @balance + em.employee_mileage_mileage as balance
FROM employee_mileage em
CROSS JOIN (select @balance := 0) init
INNER JOIN employee e
on em.employee_mileage_employee_id = e.employee_id
INNER JOIN vehicle v
on em.employee_mileage_vehicle_id = v.vehicle_id
WHERE em.employee_mileage_employee_id = " . $employee_id . "
ORDER BY em.employee_mileage_id
) as base
ORDER BY base.employee_mileage_id DESC";
$employee_mileage_query = mysqli_query($con,"{$statement} LIMIT {$startpoint} , {$per_page}") or die(mysql_error());
输出;
我建议在您的 SQL 查询中计算 运行 余额。
下面是 SQL 语句的示例:
SELECT *
from (
SELECT *,
@balance := @balance + em.employee_mileage_mileage as balance
FROM (select em.*, e.*, v.*
from employee_mileage em
INNER JOIN employee e
on em.employee_mileage_employee_id = e.employee_id
INNER JOIN vehicle v
on em.employee_mileage_vehicle_id = v.vehicle_id
WHERE em.employee_mileage_employee_id = ?
ORDER BY em.employee_mileage_id ASC
) em
CROSS JOIN (select @balance := 0) init
) as base
ORDER BY base.employee_mileage_id DESC
LIMIT ?, 2
上面语句中的参数(标有?
)应该绑定到:
- 您要显示employee_id的数据
- 当前页的起始行(从零开始)
内部查询遍历该特定员工的所有行,并将 运行 余额添加到每条记录。然后外部查询反转排序顺序并将限制应用于它以进行分页。
这个解决方案不需要在 PHP 中进行计算; 运行 余额在查询结果集中很容易获得,并且在每一页上都是正确的。
这是一个小的 SQL fiddle 演示,用于从总共 6 行中获取第二页,每页 2 行,包括正确的余额。