在给定条件

时间:2017-03-26 07:01:01

标签: mysql sql

这与我上一次question类似,但现在我的要求发生了变化,所以我发布了一个新问题。

我有以下sql架构和示例数据;

CREATE TABLE items (
  id int(11) NOT NULL AUTO_INCREMENT,
  Command varchar(10) DEFAULT NULL,
  Type tinyint(1) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO items (id,Command,Type) values(38561128,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38561099,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38561098,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38541192,'ABCD',0);
INSERT INTO items (id,Command,Type) values(38540956,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540955,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540816,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540815,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38540814,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540776,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540774,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540772,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540605,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540461,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540460,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540459,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540418,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540417,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540223,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540222,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540112,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540077,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540039,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38540021,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38539928,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38539715,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38539501,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38539500,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38539499,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38539498,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38539497,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38539496,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38539450,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38539449,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38539448,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38539217,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38539171,'HIJK',3);
INSERT INTO items (id,Command,Type) values(38539170,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538965,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538883,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538852,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538632,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538476,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38538475,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538474,'HIJK',1);
INSERT INTO items (id,Command,Type) values(38538473,'HIJK',3);
INSERT INTO items (id,Command,Type) values(38538472,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38538471,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538470,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538469,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538468,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38538467,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538419,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538418,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538417,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538416,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538415,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538413,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538244,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538243,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538242,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538022,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538021,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538020,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538019,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38538018,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38537750,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38537749,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38537564,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38537516,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38537460,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38537346,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38537185,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38536927,'HIJK',3);
INSERT INTO items (id,Command,Type) values(38536925,'HIJK',3);
INSERT INTO items (id,Command,Type) values(38536924,'HIJK',3);
INSERT INTO items (id,Command,Type) values(38536887,'HIJK',3);
INSERT INTO items (id,Command,Type) values(38536886,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38536885,'HIJK',3);
INSERT INTO items (id,Command,Type) values(38536806,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38536805,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38536804,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38536802,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38536774,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38536771,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38536770,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38536367,'HIJK',1);
INSERT INTO items (id,Command,Type) values(38536366,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38536342,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38536208,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38536207,'HIJK',1);
INSERT INTO items (id,Command,Type) values(38536206,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38536205,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38536162,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38536161,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38520155,'ABCD',0);
INSERT INTO items (id,Command,Type) values(38519878,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38519877,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38519876,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38519823,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38519822,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38519783,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38519782,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38519781,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38519306,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38518736,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38518735,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38518562,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38518462,'HIJK',3);
INSERT INTO items (id,Command,Type) values(38518409,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38518221,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38518220,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38518067,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38518066,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38518065,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38518064,'HIJK',3);
INSERT INTO items (id,Command,Type) values(38518063,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38518062,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38518061,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38518060,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38518059,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38517976,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38517975,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38517974,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38517973,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38517972,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38517971,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38517538,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38517537,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38517536,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38517480,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38517479,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38513858,'ABCD',0);
INSERT INTO items (id,Command,Type) values(38513528,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38513527,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38513286,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38513220,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38513219,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38513157,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38513156,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38513154,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38512360,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38512160,'HIJK',3);
INSERT INTO items (id,Command,Type) values(38511976,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38511749,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38511748,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38511746,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38511744,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38511743,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38511742,'ABCD',1);
INSERT INTO items (id,Command,Type) values(38511741,'HIJK',1);
INSERT INTO items (id,Command,Type) values(38511691,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38511690,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38511689,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38511405,'PQRS',0);
INSERT INTO items (id,Command,Type) values(38511404,'ABCD',1);

假设行按升序排序

我需要每组两个ID的行,其中一个是Command=ABCD, Type=0而另一个是Command=ABCD, Type=1

因此,以下样本数据应该是最终结果。

38511404,38513858 (Type 1, Type 0)
38513858,38517536 (Type 0, Type 1)
38517536,38520155 (Type 1, Type 0)
38520155,38536205 (Type 0, Type 1)
38536205,38541192 (Type 1, Type 0)
38541192,38561128 (Type 0, Type 1)

如果行是降序的话,我们应该有相同的结果(但结果行的顺序可能不同)

这是我试过的,但它没有提供所需的输出

SELECT 
  id,
  lastid,
  Command 
FROM
  (SELECT 
    id,
    Command,
    Type,
    @last_id AS lastid,
    @last_type AS lastType,
    @last_id := id,
    @last_type := Type
  FROM
    items,
    (SELECT 
      @last_id := 0,
      @last_type := 0) SQLVars 
  WHERE Command = 'ABCD' 
  ORDER BY id ASC,
    id) a 
WHERE ((a.Type = 0  AND a.lastType = 1) 
OR (a.Type = 1 AND a.lastType = 0))
ORDER BY a.id ASC 

由于

0 个答案:

没有答案