我需要让下一行也绑定到我的查询响应。例如,
SELECT * FROM `ActivityLog` WHERE data like '%865814071038085%'
这会返回我需要的记录,使用它我还需要在结果集中显示下一行
我在这个查询中获得的表记录ID是,
1,
3,
5,
7
所以我需要一种方法来获取2,4,6,8的下一行并使查询输出
1,
2,
3,
4,
5,
6,
7,
8
records.
如何使用mysql解释下一行?它总是当前行和下一个第一行。
CREATE TABLE IF NOT EXISTS `ActivityLog` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`activityAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`username` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
`userId` int(11) DEFAULT NULL,
`module` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
`controller` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
`action` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
`data` text COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `idxUserName` (`username`),
KEY `idxUserId` (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Application activity log' AUTO_INCREMENT=25390 ;
数据集,
INSERT INTO `ActivityLog` (`id`, `activityAt`, `username`, `userId`, `module`, `controller`, `action`, `data`) VALUES
(1, '2015-03-03 13:02:03', 'guest', NULL, '', 'Site', 'Login', 'Login access. IP:220.247.236.99'),
(2, '2015-03-03 13:02:08', 'QelasySuperAdmin', -1, '', 'Site', 'Login', 'Login success. IP: 220.247.236.99'),
(3, '2015-03-03 13:02:10', 'QelasySuperAdmin', -1, 'Log', 'Activity', 'Admin', 'Manage Activity Logs.'),
(4, '2015-03-03 13:02:17', 'QelasySuperAdmin', -1, 'Log', 'Activity', 'Admin', 'Manage Activity Logs.'),
(5, '2015-03-03 13:06:10', 'guest', NULL, 'Api', 'Default', 'DeviceUsage', ' Data:{"url":"http:\\/\\/www.googleadservices.com\\/pagead\\/aclk?sa=L&ai=CpOAR1LH1VPbZI4GOjAbY2ILQCeikiaQGwIKYmM8Bvtf9EwgAEAEgs_j-GCgDYIEDoAGwhoDQw&val=ChBhOTg3YjE3Y2E3MzM3NWYwEJC5jKcFGggTkt0OPC8P_iABKAAwwZ6E452z_YSAATiQuYynBUDMw9OnBQ&:","id":"1745"}'),
(6, '2015-03-03 13:06:10', 'guest', NULL, 'Api', 'Default', 'DeviceUsage', ' Response:{"code":200,"status":true,"data":[],"serverTime":"2015-03-03 13:06:10"}'),
(7, '2015-03-03 13:06:11', 'guest', NULL, 'Api', 'Default', 'DeviceUsage', ' Data:{"url":"about:blank","id":"1745"}'),
(8, '2015-03-03 13:06:11', 'guest', NULL, 'Api', 'Default', 'DeviceUsage', ' Response:{"code":200,"status":true,"data":[],"serverTime":"2015-03-03 13:06:11"}'),
(9, '2015-03-03 13:06:15', 'guest', NULL, 'Api', 'Default', 'DeviceUsage', ' Data:{"url":"http:\\/\\/clickserve","id":"1745"}'),
(10, '2015-03-03 13:06:15', 'guest', NULL, 'Api', 'Default', 'DeviceUsage', ' Response:{"code":200,"status":true,"data":[],"serverTime":"2015-03-03 13:06:15"}'))
更新
请检查此sql小提琴, http://sqlfiddle.com/#!9/b218a/1
到目前为止,没有一个答案对我有帮助。
EXPLAIN命令的输出。
答案 0 :(得分:4)
试试这个。应该很接近。
select * from
(
SELECT * FROM `ActivityLog` WHERE data like '%865814071038085%'
union
SELECT * FROM `ActivityLog` where id in (SELECT id + 1 FROM `ActivityLog` WHERE data like '%865814071038085%')
) q
演示:http://sqlfiddle.com/#!9/2b406/5
以防ids是非连续的,这是另一个:
select * from
(
SELECT * FROM `ActivityLog` WHERE data like '%FOO%'
union
SELECT * FROM `ActivityLog` where id in (SELECT min(a2.id) FROM `ActivityLog` a1 inner join `activitylog` a2 on a1.id < a2.id WHERE a1.data like '%FOO%' group by a1.id)
) q
更新小提琴:http://sqlfiddle.com/#!9/2b406/9
使用与另一个答案中的Anirban N相同的技术,但修改为仅在{NOT}下一个序列id时更新@pid
的值,并且仅在搜索到的值时匹配,或者在下一个时匹配顺序ID。 (如果id序列中存在间隙,则不会获得下一行)
select if(@pid + 1 = id, id, @pid := id) id,
activityAt,
username ,
userId,
module,
controller,
action,
data
from `ActivityLog` as a
where a.data like '%865814071038085%'
or id = @pid + 1;
答案 1 :(得分:0)
可以将结果存储在MySql变量(@pid)中,并在下一个联合查询中使用它,
工作示例
编辑版本2 (SQL FIDDLE)
SET @pid:= 0;
SELECT @pid:=id, `activityAt` ,`username` , `userId` , `module` ,`controller`,`action` ,`data`
FROM `ActivityLog` as a WHERE a.data like '%865814071038085%' OR @pid!=0;
答案 2 :(得分:0)
这应该可以在下一个更高的Id上自行加入表格。
SELECT * FROM
(
SELECT * FROM `ActivityLog`
WHERE data LIKE '%865814071038085%'
UNION
SELECT *
FROM `ActivityLog`
WHERE id IN (
SELECT t2.id
FROM `ActivityLog` t1
JOIN `ActivityLog` t2
ON t2.id = t1.id+1
)
)t
ORDER BY Id
<强> SQL FIDDLE 强>