我有MySQL表与下面的值。取IDS连续两行其列具有相同的值
id model category Code item_type
--------------------------------------
543 XYZ PQR ABC12 0
535 XYZ PQR ABC12 1
532 XYZ PQR ABC12 0
528 XYZ PQR ABC12 0
524 XYZ PQR ABC12 1
518 XYZ PQR ABC12 0
515 XYZ PQR ABC12 1
510 XYZ PQR ABC12 0
508 XYZ PQR QRP24 0
495 XYZ PQR QRP24 0
-- -- -- -- --
实施例的架构和数据
CREATE TABLE `test_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`model` varchar(5) DEFAULT NULL,
`category` varchar(5) DEFAULT NULL,
`code` varchar(5) DEFAULT NULL,
`item_tpe` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=544 DEFAULT CHARSET=utf8;
insert into `test_table`(`id`,`model`,`category`,`code`,`item_tpe`) values (495,'XYZ','PQR','QRP24',0);
insert into `test_table`(`id`,`model`,`category`,`code`,`item_tpe`) values (508,'XYZ','PQR','QRP24',0);
insert into `test_table`(`id`,`model`,`category`,`code`,`item_tpe`) values (510,'XYZ','PQR','ABC12',0);
insert into `test_table`(`id`,`model`,`category`,`code`,`item_tpe`) values (515,'XYZ','PQR','ABC12',1);
insert into `test_table`(`id`,`model`,`category`,`code`,`item_tpe`) values (518,'XYZ','PQR','ABC12',0);
insert into `test_table`(`id`,`model`,`category`,`code`,`item_tpe`) values (524,'XYZ','PQR','ABC12',1);
insert into `test_table`(`id`,`model`,`category`,`code`,`item_tpe`) values (528,'XYZ','PQR','ABC12',0);
insert into `test_table`(`id`,`model`,`category`,`code`,`item_tpe`) values (532,'XYZ','PQR','ABC12',0);
insert into `test_table`(`id`,`model`,`category`,`code`,`item_tpe`) values (535,'XYZ','PQR','ABC12',1);
insert into `test_table`(`id`,`model`,`category`,`code`,`item_tpe`) values (543,'XYZ','PQR','ABC12',0);
问题
要求是,我需要获取ids
为这两个记录其item_type
是0且这些行必须是连续的,只要它们共享相同的Code
。
因此,例如,在 上面的表格,所需的ID是532 and 528
,508 and 495
等,可能有很多。
更新
示例输出为上述表应该是一个数组
[532,528] if `code='ABC12'` and `[508, 495]` if the `code='QRP24'`
更新2 当code
值赋予
任何帮助期望的输出应该被撷取将高度赞赏
你是什么意思的“连续”?你有什么订单,ID DESC? – 2oppin
你可以请样品输出吗? –
@ 2oppin,ID是降序,'连续',这里我的意思是两行来一个接一个'item_type = 0'并且它们具有相同的Column值'Code' – WatsMyName