目前正在一个项目中工作,我需要从具有不同条件的同一列中的多行中过滤唯一的产品ID
CREATE TABLE IF NOT EXISTS `spec_property` (
`sp_id` int(11) unsigned NOT NULL,
`spm_id` int(11) unsigned NOT NULL,
`spec_value` varchar(255) NOT NULL,
`spec_last_value` varchar(55) DEFAULT NULL,
`product_id` int(11) unsigned NOT NULL,
`spec_order_by` tinyint(2) unsigned DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=77 ;
ALTER TABLE `spec_property`
ADD PRIMARY KEY (`sp_id`), ADD KEY `product_id` (`product_id`), ADD KEY `spec_value` (`spec_value`);
ALTER TABLE `spec_property`
MODIFY `sp_id` int(11) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=75;
INSERT INTO `spec_property` (`sp_id`, `spm_id`, `spec_value`, `spec_last_value`, `product_id`, `spec_order_by`) VALUES
(31, 23, 'Octa Core', NULL, 4, NULL),
(34, 26, '2048', 'MB', 4, NULL),
(35, 27, '8192', 'MB', 4, NULL),
(36, 28, '2048', 'MB', 4, NULL),
(69, 62, 'Android', NULL, 4, NULL),
(70, 63, '5', NULL, 4, NULL),
(71, 65, 'Yes', NULL, 4, NULL),
(72, 66, 'Yes', NULL, 4, NULL),
(73, 67, '32000', NULL, 4, NULL),
(74, 68, '25', 'fps', 4, NULL);CREATE TABLE IF NOT EXISTS `spec_property` (
`sp_id` int(11) unsigned NOT NULL,
`spm_id` int(11) unsigned NOT NULL,
`spec_value` varchar(255) NOT NULL,
`spec_last_value` varchar(55) DEFAULT NULL,
`product_id` int(11) unsigned NOT NULL,
`spec_order_by` tinyint(2) unsigned DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=77 ;
ALTER TABLE `spec_property`
ADD PRIMARY KEY (`sp_id`), ADD KEY `product_id` (`product_id`), ADD KEY `spec_value` (`spec_value`);
ALTER TABLE `spec_property`
MODIFY `sp_id` int(11) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=75;
INSERT INTO `spec_property` (`sp_id`, `spm_id`, `spec_value`, `spec_last_value`, `product_id`, `spec_order_by`) VALUES
(31, 23, 'Octa Core', NULL, 4, NULL),
(34, 26, '2048', 'MB', 4, NULL),
(35, 27, '8192', 'MB', 4, NULL),
(36, 28, '2048', 'MB', 4, NULL),
(69, 62, 'Android', NULL, 4, NULL),
(70, 63, '5', NULL, 4, NULL),
(71, 65, 'Yes', NULL, 4, NULL),
(72, 66, 'Yes', NULL, 4, NULL),
(73, 67, '32000', NULL, 4, NULL),
(74, 68, '25', 'fps', 4, NULL);
http://sqlfiddle.com/#!9/95329/3
SELECT distinct(product_id)
FROM `spec_property`
WHERE (spm_id = '62' AND spec_value IN ('Android' , 'Windows'))
AND (spm_id = '27' AND spec_value BETWEEN 4096 AND 131072 )
如果我使用' OR'条件我可以得到这个结果:
(http://sqlfiddle.com/#!9/95329/1)。实际上我需要找到与
等过滤器选项匹配的产品ID从spec_property中选择distinct(product_id),其中spm_id_62 IN (' Android',' Windows')和spm_id_27 BETWEEN 4096和131072
答案 0 :(得分:0)
使用OR
info
答案 1 :(得分:0)
如果您正在寻找一种方法来查找符合所有过滤条件的产品,可以使用self join solution。
另一种查找与所有过滤器匹配的产品的方法,假设spm_id对于每个产品ID都是唯一的。
SELECT product_id
FROM `spec_property`
where (spm_id = '62' and spec_value in ('Android' , 'Windows'))
or (spm_id = '27' and spec_value between 4096 and 131072 )
group by product_id having count(*) = 2; -- number of filters
答案 2 :(得分:0)
你有没有想到这个?
select distinct(a.product_id) from `spec_property` a
inner join `spec_property` b on
a.product_id=b.product_id and a.spm_id = '62' and a.spec_value in
('Android' , 'Windows') and b.spm_id = '27' and
b.spec_value between 4096 and 131072