我尝试实现车辆部件产品过滤,其中products
属于许多不同的fitments
。比如特定的灯泡可以属于雪佛兰,福特和本田汽车。
我遇到的问题是使用AND而OR只是不能正常工作。
以下是我要查找的结果的伪代码(表格结构和数据位于底部):
SELECT * FROM products
JOIN fitments ON fitments.sku = products.sku
WHERE ANY fitments.make (grouped by sku) MATCHES ALL 'Ford' AND 'Chevrolet' AND 'Honda'
AND products.active = 1
AND fitments.cat_name = 'Cars'
GROUP BY products.sku
福特,雪佛兰和本田并不存在于同一行,所以我不能把它变成一个WHERE子句?
OR
无法正常工作,因为我不想返回不符合所有条件的结果。
AND
不起作用,因为不是每一行都包含所有三个makes
并且会返回0行。
我试图实现的结果:
我需要帮助的查询结果会返回一行products
行(不是fitments
,因为fitments
只是关系查找),BULB2
因为匹配所有可能的装备中有三个make
参数(福特,雪佛兰和本田)。
两个表,产品和配件:
CREATE TABLE `fitments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`cat_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`sku` varchar(14) COLLATE utf8_unicode_ci DEFAULT NULL,
`make` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`model` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`start_year` varchar(4) COLLATE utf8_unicode_ci DEFAULT NULL,
`end_year` varchar(4) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30211 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `products` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`sku` varchar(14) COLLATE utf8_unicode_ci DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`active` tinyint(1) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=63730 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `fitments` (`id`, `cat_name`, `sku`, `make`, `model`, `start_year`, `end_year`, `created_at`, `updated_at`) VALUES ('1', 'Cars', 'BULB1', 'Ford', 'F150', '2013', '2015', '0000-00-00 00:00:00', '0000-00-00 00:00:00');
INSERT INTO `fitments` (`id`, `cat_name`, `sku`, `make`, `model`, `start_year`, `end_year`, `created_at`, `updated_at`) VALUES ('2', 'Cars', 'BULB2', 'Ford', 'Explorer', '2013', '2015', '0000-00-00 00:00:00', '0000-00-00 00:00:00');
INSERT INTO `fitments` (`id`, `cat_name`, `sku`, `make`, `model`, `start_year`, `end_year`, `created_at`, `updated_at`) VALUES ('3', 'Cars', 'BULB1', 'Ford', 'Mustang', '2013', '2015', '0000-00-00 00:00:00', '0000-00-00 00:00:00');
INSERT INTO `fitments` (`id`, `cat_name`, `sku`, `make`, `model`, `start_year`, `end_year`, `created_at`, `updated_at`) VALUES ('4', 'Cars', 'BULB2', 'Chevrolet', 'Cobalt', '2013', '2015', '0000-00-00 00:00:00', '0000-00-00 00:00:00');
INSERT INTO `fitments` (`id`, `cat_name`, `sku`, `make`, `model`, `start_year`, `end_year`, `created_at`, `updated_at`) VALUES ('5', 'Cars', 'BULB3', 'Chevrolet', 'Corvette', '2013', '2015', '0000-00-00 00:00:00', '0000-00-00 00:00:00');
INSERT INTO `fitments` (`id`, `cat_name`, `sku`, `make`, `model`, `start_year`, `end_year`, `created_at`, `updated_at`) VALUES ('6', 'Cars', 'BULB2', 'Honda', 'Civic', '2013', '2015', '0000-00-00 00:00:00', '0000-00-00 00:00:00');
INSERT INTO `products` (`id`, `sku`, `name`, `active`, `created_at`, `updated_at`) VALUES ('1', 'BULB1', 'Generic Bulb 1', '1', '0000-00-00 00:00:00', '0000-00-00 00:00:00');
INSERT INTO `products` (`id`, `sku`, `name`, `active`, `created_at`, `updated_at`) VALUES ('2', 'BULB2', 'Generic Bulb 2', '1', '0000-00-00 00:00:00', '0000-00-00 00:00:00');
INSERT INTO `products` (`id`, `sku`, `name`, `active`, `created_at`, `updated_at`) VALUES ('3', 'BULB3', 'Generic Bulb 3', '1', '0000-00-00 00:00:00', '0000-00-00 00:00:00');
答案 0 :(得分:1)
SELECT sku
FROM fitments
WHERE make IN('Ford', 'Chevrolet', 'Honda')
GROUP
BY sku HAVING COUNT(DISTINCT make) = 3;
答案 1 :(得分:0)
答案很容易,如果你改写一下你的问题:不要说"加入福特和雪佛兰和本田"而只是说"加入福特并加入雪佛兰AND加入本田":
SELECT
products.*
-- , whatever.column you need else
FROM
products
INNER JOIN fitments AS fitmentsFord ON products.sku=fitmentsFord.sku
INNER JOIN fitments AS fitmentsChevrolet ON products.sku=fitmentsChevrolet.sku
INNER JOIN fitments AS fitmentsHonda ON products.sku=fitmentsHonda.sku
WHERE
fitmentsFord.make='Ford'
AND fitmentsChevrolet.make='Chevrolet'
AND fitmentsHonda.make='Honda'
AND products.active = 1
AND fitments.cat_name = 'Cars'
GROUP BY products.sku
答案 2 :(得分:0)
试试这个::
SELECT p.sku FROM products p
INNER JOIN
(Select * from fitments group by make, sku) f ON f.sku = p.sku
group by p.sku having count(f.make)=3