2016-11-28 67 views
0

我有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 528508 and 495等,可能有很多。

更新

示例输出为上述表应该是一个数组

[532,528] if `code='ABC12'` and `[508, 495]` if the `code='QRP24'` 

更新2code值赋予

任何帮助期望的输出应该被撷取将高度赞赏

+0

你是什么意思的“连续”?你有什么订单,ID DESC? – 2oppin

+0

你可以请样品输出吗? –

+0

@ 2oppin,ID是降序,'连续',这里我的意思是两行来一个接一个'item_type = 0'并且它们具有相同的Column值'Code' – WatsMyName

回答

1

您可以使用变量来跟踪你的前一行。这在MySQL中并不漂亮,但是可以工作:

SELECT id, lastid, code FROM (
    SELECT 
     id, 
     code, 
     item_tpe, 
     @last_id as lastid, 
     @last_code as lastcode, 
     @last_item_tpe as lastitemtpe, 
     @last_id:=id, 
     @last_code:=code, 
     @last_item_tpe:=item_tpe 
    FROM 
     test_table, 
     (select 
      @last_id := 0, 
      @last_code := "", 
      @last_item_tpe:=0 
     ) SQLVars 
    ORDER BY code, id 
) a 
WHERE 
    a.code = a.lastcode 
    AND a.item_tpe = 0 
    AND a.lastitemtpe = 0 
+0

谢谢,但它不能正常工作,它不能获取所有有效的行 – WatsMyName

+0

你能给我一个它不起作用的例子吗?对于您提供的测试数据,它将返回两个预期对。 – fafl

+0

嘿,这里是测试数据的链接,已经上传到mediafire - http://www.mediafire.com/file/xv6yme6fswvybqu/testdata.sql – WatsMyName

1

有几种方法。这里有一个:

CREATE TABLE `test_table` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `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`,`code`,`item_tpe`) values 
(495,'QRP24',0), 
(508,'QRP24',0), 
(510,'ABC12',0), 
(515,'ABC12',1), 
(518,'ABC12',0), 
(524,'ABC12',1), 
(528,'ABC12',0), 
(532,'ABC12',0), 
(535,'ABC12',1), 
(543,'ABC12',0); 

SELECT a.* 
    FROM 
    (SELECT x.* 
      , MIN(y.id) next 
     FROM test_table x 
     JOIN test_table y 
      ON y.id > x.id 
     GROUP 
      BY x.id 
    ) a 
    JOIN test_table b 
    ON b.id = a.next 
    AND b.code = a.code 
    AND b.item_tpe = a.item_tpe 
WHERE a.item_tpe = 0; 
+-----+-------+----------+------+ 
| id | code | item_tpe | next | 
+-----+-------+----------+------+ 
| 495 | QRP24 |  0 | 508 | 
| 528 | ABC12 |  0 | 532 | 
+-----+-------+----------+------+ 

东西沿着这些路线也可以工作...

SELECT * FROM 
(
SELECT x.* 
    , CASE WHEN @prev_code = x.code THEN 
     CASE WHEN @prev_item_tpe = item_tpe THEN @status:='true'ELSE @status:='false' END 
      ELSE @status := 'false'END status 
    , @prev_code := x.code 
    , @prev_item_tpe := x.item_tpe 
    FROM test_table x 
    , (SELECT @prev_code := null, @prev_item_tpe := null, @status := null) vars 
ORDER 
    BY x.id 
) a 
WHERE status = 'true' AND item_tpe = 0; 

编辑:在您放大的数据集快速测试显示,这两个查询返回相同的行...

SELECT a.id  
    , a.item_tpe 
    , a.code  
    , a.next 
    FROM 
    (SELECT x.* 
      , MIN(y.id) next 
     FROM test_table x 
     JOIN test_table y 
      ON y.id > x.id 
     GROUP 
      BY x.id 
    ) a 
    JOIN test_table b 
    ON b.id = a.next 
    AND b.code = a.code 
    AND b.item_tpe = a.item_tpe 
WHERE a.item_tpe = 0; 
+--------+----------+------------+--------+ 
| id  | item_tpe | code  | next | 
+--------+----------+------------+--------+ 
| 491209 |  0 | 3066754917 | 491210 | 
| 491210 |  0 | 3066754917 | 491211 | 
| 491211 |  0 | 3066754917 | 491212 | 
+--------+----------+------------+--------+ 

SELECT a.id  
    , a.item_tpe 
    , a.code  
    , a.status 
    FROM 
(
SELECT x.* 
    , CASE WHEN @prev_code = x.code THEN 
     CASE WHEN @prev_item_tpe = item_tpe THEN @status:='true'ELSE @status:='false' END 
      ELSE @status := 'false'END status 
    , @prev_code := x.code 
    , @prev_item_tpe := x.item_tpe 
    FROM test_table x 
    , (SELECT @prev_code := null, @prev_item_tpe := null, @status := null) vars 
ORDER 
    BY x.id 
) a 
WHERE status = 'true' AND item_tpe = 0; 
+--------+----------+------------+--------+ 
| id  | item_tpe | code  | status | 
+--------+----------+------------+--------+ 
| 491210 |  0 | 3066754917 | true | 
| 491211 |  0 | 3066754917 | true | 
| 491212 |  0 | 3066754917 | true | 
+--------+----------+------------+--------+ 
+0

谢谢,第一个解决方案工作,如果有较少的数据,但对于大表其查询了永远执行。第二个解决方案只是检索一个ID,另一个相关的ID不会被检索,如第一个解决方案中的“next” – WatsMyName

+0

我会认为(code,type)上的索引相当快。 – Strawberry

+0

它甚至是慢得多当只有10000表 – WatsMyName

相关问题