我有这张桌子:
CREATE TABLE IF NOT EXISTS `razorphyn_support_list_messages` (
`id` BIGINT(15) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` BIGINT(11) UNSIGNED NOT NULL,
`message` TEXT NOT NULL,
`ticket_id` BIGINT(11) UNSIGNED NOT NULL,
`ip_address` VARCHAR(20) NOT NULL,
`created_time` DATETIME NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=20;
我使用此查询选择行(仅第一次):
SELECT `user_id`,`message`,`created_time`
FROM ".$SupportMessagesTable."
WHERE `ticket_id`=?
ORDER BY created_time DESC LIMIT 10
这很好,但是在我调用一个检索其他行的 AJAX 脚本后,该函数会向 PHP 脚本发送一些信息,并且在它们之间还有一个 var(偏移量),每次我从服务器获得肯定响应时都会增加。
问题是我想显示从最新到最旧的记录,但我不知道如何从查询中排除“最后一行 - 最晚”:
$query = "SELECT `user_id`,`message`,`created_time`
FROM ".$SupportMessagesTable."
WHERE ticket_id=? AND 'last row'-".$offset.")
ORDER BY created_time DESC LIMIT ".$postnumbers;
我想过这样的事情,但显然它是错误的(不幸的是我不太了解mysql):
"SELECT `user_id`,`message`,`created_time`
FROM ".$SupportMessagesTable."
WHERE ticket_id=?
ORDER BY created_time DESC
LIMIT
((SELECT COUNT(*)
FROM ".$SupportMessagesTable."
WHERE ticket_id=?)-".$offset."),".$postnumbers;
$postnumbers
等于 10,第一个值为$offset
21,每次成功调用后将递增 10
这些是记录:
id,user_id,message,ticket_id,ip address, created_time
(1, 55, '<p>ciao ciao</p> <p>ciao</p> ', 41, '127.0.0.1', '2013-05-15 06:26:01'),
(2, 55, '<p>ciao ciao</p> <p>ciao</p> ', 41, '127.0.0.1', '2013-05-16 09:29:43'),
(3, 55, '1', 41, '127.0.0.1', '2013-05-16 17:06:01'),
(4, 55, '2', 41, '127.0.0.1', '2013-05-16 17:07:01'),
(5, 55, '3', 41, '127.0.0.1', '2013-05-16 17:08:01'),
(6, 55, '4', 41, '127.0.0.1', '2013-05-16 17:09:01'),
(7, 55, '9', 49, '127.0.0.1', '2013-05-16 17:14:01'),
(8, 55, '5', 41, '127.0.0.1', '2013-05-16 17:10:01'),
(9, 55, '6', 41, '127.0.0.1', '2013-05-16 17:11:01'),
(10, 55, '7', 41, '127.0.0.1', '2013-05-16 17:12:01'),
(11, 55, '8', 41, '127.0.0.1', '2013-05-16 17:13:01'),
(12, 55, '9', 41, '127.0.0.1', '2013-05-16 17:14:01'),
(13, 55, '9', 49, '127.0.0.1', '2013-05-16 17:14:01'),
(14, 55, '10', 41, '127.0.0.1', '2013-05-16 17:15:01');
第一次运行时,我将选择 ids 14 和 12 到 3,除了 7(不同的票证 id),第二次查询我想选择 id 1 和 2
可能最简单的解决方案是检索$postnumbers*number of calls
,然后while
只循环十个,但我认为这不会很有效