我有值为
的mysql架构示例架构和数据
CREATE TABLE `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`items` varchar(25) DEFAULT NULL,
`itemtype` tinyint(1) DEFAULT NULL,
`category` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=45 DEFAULT CHARSET=latin1;
insert into `products` (`id`, `items`, `itemtype`, `category`) values('1','APPLE','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('2','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('3','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('4','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('5','APPLE','1','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('6','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('7','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('8','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('9','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('10','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('11','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('12','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('13','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('14','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('15','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('16','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('17','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('18','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('19','APPLE','1','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('20','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('21','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('22','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('23','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('24','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('25','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('26','APPLE','1','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('27','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('28','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('29','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('30','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('31','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('32','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('33','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('34','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('35','APPLE','1','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('36','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('37','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('38','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('39','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('40','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('41','APPLE','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('42','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('43','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('44','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('45','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('46','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('47','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('48','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('49','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('50','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('51','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('52','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('53','APPLE','1','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('54','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('55','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('56','MANGO','0','FM1010');
insert into `products` (`id`, `items`, `itemtype`, `category`) values('57','APPLE','1','FM1010');
问题
要求是我需要获取位于具有item=APPLE
和itemtype=1
item=APPLE, itemtype=0
和item=APPLE, itemtype=1
的所有行的ID
因此,在示例数据中,id=5
(Apple,1)和' id = 41' (Apple,0),有3行包含项目类型为1的项目APPLE(具有ID为19,26和35的行)。
类似地,在id=41
(Apple,0)和id=57
(Apple,1)之间,还有2行具有项目类型为1的项目APPLE(具有ID 45和53的行)。
因此所需的输出标识为19,26,35,45,53
更新
解释
第一个边界是id为1(Apple,0)的行,用于查找下一个边界 该行应为APPLE,1,在这种情况下,行为id 5.现在介于1之间 5没有Apple,1所以我们忽略了这个。
现在我们将行id 5(Apple,1)作为第一个边界,即下一个边界 应该是带有APPLE的行,0,即第41行。
现在我们有3行APPLE,这两行之间有1行。 19,26和35
现在第一个边界是41 Apple,0,现在下一个边界应该是APPLE,1 这是行ID 53.这两者之间没有APPLE,1,所以我们忽略它
所以最终结果应该是19,26,35
有几个数据,我们必须匹配APPLE,0和APPLE之间的所有APPLE,1
已更新 我修改了架构和问题
答案 0 :(得分:2)
尝试此查询
SELECT *
FROM products
WHERE items = 'apple'
AND itemtype = '1'
AND id >
(
SELECT MIN(id) AS id
FROM products
WHERE items = 'apple'
AND itemtype = '1'
)
AND id <
(
SELECT MIN(id)
FROM products
WHERE items = 'apple'
AND itemtype = '0'
AND id >
(
SELECT MIN(id) AS id
FROM products
WHERE items = 'apple'
AND itemtype = '1'
)
);