简体   繁体   English

如何将产品计数到多个过滤器

[英]How to count products to multiple filters

UPDATED更新

I like to make product filter module like this one Online Shop , when you click one of the filters to count current products in other filters我喜欢制作这样一个网上商店的产品过滤器模块,当您单击其中一个过滤器以计算其他过滤器中的当前产品时

For example if this is frontend and their are checkboxes and their are UNchecked atm例如,如果这是前端,它们是复选框,它们是未选中的 atm

Size (group_id)
  10m (option_id: 52) (21 products)
  20m (option_id: 51) (1 product)

Color (group_id)
   Green (option_id: 49) (22 products)
   Black (option_id: 38) (1 product)

If a user select 10m (option_id: 52) the filter counter should become like this如果用户选择 10m (option_id: 52) 过滤器计数器应该变成这样

Size (group_id)
  10m (option_id: 52) (21 products)
  20m (option_id: 51) (1)

Color (group_id)
   Green (option_id: 49) (2)
   Black (option_id: 38) (0)

I made one table only for counting where i keep group_id,option_id,product_id,category_id,manufacturer_id我制作了一张表格,仅用于计算保留 group_id、option_id、product_id、category_id、manufacturer_id 的位置

I make query that count products to filter but problem is that i cant return other filter counter (option_id: 38) in same category and manufacturer counter for all manufacturer in same category , any ideas how to make it ?我查询要过滤的产品数量,但问题是我无法返回同一类别中的其他过滤器计数器(option_id:38)和同一类别中所有制造商的制造商计数器,有什么想法如何制作吗?

SELECT sd.*, COUNT(sd.product_id) FROM filter_counter sd WHERE sd.product_id IN (SELECT c.product_id FROM filter_counter c WHERE c.option_id IN (52) AND c.category_id = 127) AND sd.category_id = 127 GROUP BY sd.option_id

CREATE TABLE `filter_counter` (
  `id` int(11) NOT NULL,
  `group_id` int(11) NOT NULL,
  `option_id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `category_id` int(11) NOT NULL,
  `manufacturer_id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `filter_counter` (`id`, `group_id`, `option_id`, `product_id`, `category_id`, `manufacturer_id`) VALUES
(1, 33, 52, 5124, 65, 36),
(2, 33, 52, 5124, 127, 36),
(3, 33, 52, 5125, 65, 36),
(4, 33, 52, 5125, 127, 36),
(5, 33, 52, 5138, 65, 36),
(6, 33, 52, 5138, 127, 36),
(7, 33, 52, 5141, 65, 36),
(8, 33, 52, 5141, 127, 36),
(9, 33, 52, 5146, 65, 36),
(10, 33, 52, 5146, 127, 36),
(11, 33, 52, 5147, 65, 36),
(12, 33, 52, 5147, 127, 36),
(13, 33, 52, 5148, 65, 36),
(14, 33, 52, 5148, 127, 36),
(15, 33, 52, 5149, 65, 36),
(16, 33, 52, 5149, 127, 36),
(17, 33, 52, 5150, 65, 36),
(18, 33, 52, 5150, 127, 36),
(19, 33, 52, 5151, 65, 36),
(20, 33, 52, 5151, 127, 36),
(21, 33, 52, 5152, 65, 36),
(22, 33, 52, 5152, 127, 36),
(23, 33, 52, 5153, 65, 36),
(24, 33, 52, 5153, 127, 36),
(25, 33, 52, 5154, 65, 36),
(26, 33, 52, 5154, 127, 36),
(27, 33, 52, 5155, 65, 36),
(28, 33, 52, 5155, 127, 36),
(29, 33, 52, 5156, 65, 36),
(30, 33, 52, 5156, 127, 36),
(31, 33, 52, 5157, 65, 36),
(32, 33, 52, 5157, 127, 36),
(33, 33, 52, 7042, 65, 38),
(34, 33, 52, 7042, 127, 38),
(35, 33, 52, 7048, 65, 38),
(36, 33, 52, 7048, 127, 38),
(37, 33, 52, 7124, 65, 0),
(38, 33, 52, 7124, 127, 0),
(39, 32, 49, 7185, 65, 0),
(40, 32, 49, 7185, 127, 0),
(41, 32, 49, 7517, 65, 39),
(42, 32, 49, 7517, 127, 39),
(43, 32, 49, 7518, 65, 39),
(44, 32, 49, 7518, 127, 39),
(45, 32, 49, 7538, 65, 39),
(46, 32, 49, 7538, 127, 39),
(47, 32, 49, 7657, 65, 39),
(48, 32, 49, 7657, 127, 39),
(49, 32, 49, 7658, 65, 39),
(50, 32, 49, 7658, 127, 39),
(51, 32, 49, 7797, 65, 21),
(52, 32, 49, 7797, 127, 21),
(53, 32, 49, 7798, 65, 21),
(54, 32, 49, 7798, 127, 21),
(55, 32, 49, 7799, 65, 21),
(56, 32, 49, 7799, 127, 21),
(57, 32, 49, 7800, 65, 21),
(58, 32, 49, 7800, 127, 21),
(59, 32, 49, 7801, 65, 21),
(60, 32, 49, 7801, 127, 21),
(61, 32, 49, 7802, 65, 21),
(62, 32, 49, 7802, 127, 21),
(63, 32, 49, 7803, 65, 21),
(64, 32, 49, 7803, 127, 21),
(65, 32, 49, 7804, 65, 21),
(66, 32, 49, 7804, 127, 21),
(67, 32, 49, 7805, 65, 21),
(68, 32, 49, 7805, 127, 21),
(69, 32, 49, 7806, 65, 21),
(70, 32, 49, 7806, 127, 21),
(71, 32, 49, 7807, 65, 21),
(72, 32, 49, 7807, 127, 21),
(73, 32, 49, 7808, 65, 21),
(74, 32, 49, 7808, 127, 21),
(75, 32, 49, 7809, 65, 21),
(76, 32, 49, 7809, 127, 21),
(77, 32, 49, 7810, 65, 21),
(78, 32, 49, 7810, 127, 21),
(79, 29, 38, 7811, 65, 21),
(80, 29, 38, 7811, 127, 21),
(81, 32, 49, 8020, 65, 21),
(82, 32, 49, 8020, 127, 21),
(83, 33, 52, 8020, 65, 21),
(84, 33, 52, 8020, 127, 21),
(85, 32, 49, 8021, 65, 21),
(86, 32, 49, 8021, 127, 21),
(87, 33, 51, 8021, 65, 21),
(88, 33, 51, 8021, 127, 21),
(89, 33, 52, 8021, 65, 21),
(90, 33, 52, 8021, 127, 21);


My database and query example我的数据库和查询示例

For example if this is frontend and their are checkboxes and their are UNchecked atm例如,如果这是前端,它们是复选框,它们是未选中的 atm

 Size (group_id) 10m (option_id: 52) (21 products) 20m (option_id: 51) (1 product) Color (group_id) Green (option_id: 49) (22 products) Black (option_id: 38) (1 product)
SELECT COUNT(DISTINCT CASE WHEN option_id = 52 THEN product_id END) p52,
       COUNT(DISTINCT CASE WHEN option_id = 51 THEN product_id END) p51,
       COUNT(DISTINCT CASE WHEN option_id = 49 THEN product_id END) p49,
       COUNT(DISTINCT CASE WHEN option_id = 38 THEN product_id END) p38
FROM filter_counter;

If a user select 10m (option_id: 52) the filter counter should become like this如果用户选择 10m (option_id: 52) 过滤器计数器应该变成这样

Size (group_id) 10m (option_id: 52) (21 products) 20m (option_id: 51) (1) Color (group_id) Green (option_id: 49) (2) Black (option_id: 38) (0)
SELECT COUNT(DISTINCT CASE WHEN option_id = 52 THEN product_id END) p52,
       COUNT(DISTINCT CASE WHEN option_id = 51 THEN product_id END) p51,
       COUNT(DISTINCT CASE WHEN option_id = 49 THEN product_id END) p49,
       COUNT(DISTINCT CASE WHEN option_id = 38 THEN product_id END) p38
FROM filter_counter
JOIN ( SELECT DISTINCT product_id
       FROM filter_counter
       WHERE option_id IN (52) ) filter1 USING (product_id);

fiddle 小提琴

If user checks additional filter in the same group then the next value is added into the condition.如果用户检查同一组中的其他过滤器,则将下一个值添加到条件中。 For example, if he checks option_id = 51 which is in the same group with option_id = 52 then filtering subquery become例如,如果他检查option_id = 51option_id = 52在同一组中,则过滤子查询变为

JOIN ( SELECT DISTINCT product_id
       FROM filter_counter
       WHERE option_id IN (52, 51) ) filter1 USING (product_id)

If user checks additional filter in another group then additional filterX subquery added.如果用户在另一个组中检查附加过滤器,则添加附加filterX子查询。 For example, if he checks option_id = 49 then the next subquery added to the query:例如,如果他检查option_id = 49那么下一个子查询添加到查询中:

JOIN ( SELECT DISTINCT product_id
       FROM filter_counter
       WHERE option_id IN (49) ) filter2 USING (product_id)

is there any way output to become one per line ?有没有办法让每行输出变成一个?

SELECT options.option_id,
       COUNT(DISTINCT CASE WHEN filter_counter.option_id = options.option_id 
                           THEN product_id 
                           END) option_count
FROM filter_counter
CROSS JOIN ( SELECT DISTINCT option_id
             FROM filter_counter ) options
JOIN ( SELECT DISTINCT product_id
       FROM filter_counter
       WHERE option_id IN (52) ) filter1 USING (product_id)
GROUP BY options.option_id;

fiddle 小提琴

Sadly I cannot make a lot of sense out of your example:可悲的是,我无法从您的示例中获得很多意义

for example if i have one product_id 8020 with two options ( options_id 49 and 52 ) when user select option 49 count must show 1 product in option 49 and 1 product in option 52例如,如果我有一个 product_id 8020 有两个选项( options_id 49 和 52 ),当用户选择选项 49 计数必须显示选项 49 中的 1 个产品和选项 52 中的 1 个产品

But following your title, ONE TO MANY and your problem:但是按照您的标题,一对多和您的问题:

But the problem is coming when you have to select one or more filters how to count how many products are sill enable to combinate between filters ?但问题来了,当您必须选择一个或多个过滤器时,如何计算有多少产品可以在过滤器之间进行组合?

I will try to explain with a generalised answer.我将尝试用一个概括的答案来解释。

First you have to figure out what makes a product able to fit in the two filters you have selected.首先,您必须弄清楚是什么使产品能够适合您选择的两个过滤器。 If you SELECT filter_id '1' and '5'如果您选择 filter_id '1''5'

(1, 32, 49, 8020, 65, 21)

(5, 32, 49, 8021, 65, 21)

We can see that the only thing they differ in is the product_id so if you want to select all products who share everything except their product_id you can do a very basic SELECT product_id, COUNT(product_id) AS total FROM filter_counter WHERE group_id = 32 AND category_id = 65我们可以看到它们唯一不同的是product_id所以如果你想选择所有共享除product_id之外的所有内容的产品,你可以做一个非常基本的SELECT product_id, COUNT(product_id) AS total FROM filter_counter WHERE group_id = 32 AND category_id = 65

But if you want to make the entire solution based on a ONE TO MANY as mentioned in your title I would suggest you create a new table for your products and make every product have a filter id instead.但是,如果您想根据标题中提到的一对多来制作整个解决方案,我建议您为您的产品创建一个新表,并让每个产品都有一个过滤器 ID。 Then you can count the amount of products who share the same filter.然后您可以计算共享相同过滤器的产品数量。 You can read more about it in this stackoverflow question: How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?您可以在这个 stackoverflow 问题中阅读更多相关信息: 如何在设计表时实现一对一、一对多和多对多关系?

Example:例子:

CREATE TABLE 'product' (
   product_id PRIMARY KEY,
   filter_id int REFERENCES filter_counter(id),
   ... More info for product
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM