我正在尝试实现车辆零件产品过滤,其中有属于许多不同的productsfitments。比如某个灯泡可以属于雪佛兰、福特和本田汽车。
我现在遇到的问题是如何使用和和或根本不起作用。
下面是我要查找的结果的伪代码(表结构和数据位于底部):

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');

最佳答案

如果你重新表述你的问题,答案很容易得到:不要说“加入了福特、雪佛兰和本田”,只要说“加入了福特、雪佛兰和本田”:

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

09-25 21:06