次の値を持つMySqlテーブルがあります。列が同じ値を持つ2つの連続する行のIDを取得する
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);
要件は、私がitem_type
0であり、これらの行が連続している必要があり、これらの2つのレコードのids
をフェッチする必要があるということです問題、同じものを共有している場合は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」を持ち、同じコード値を持つ2つの行が続きます。 – WatsMyName