简体   繁体   English

过滤产品的一个类别,但带走所有类别的过滤产品

[英]Filter one category of a product, but bring all categories of the filtered products

Here's the thing. 就是这个

I have a query that brings all products that I have in my database. 我有一个查询,该查询将数据库中包含的所有产品带入。 Those products are of one or more categories. 这些产品属于一种或多种类别。 So I have 3 tables. 所以我有3张桌子。
- Product table -产品表
- Category table -类别表
- Table that holds the relations between those two. -包含两者之间关系的表格。

I use GROUP_CONCAT to get the names of all categories from which a product belongs when getting the products and it works fine. 获取产品时,我使用GROUP_CONCAT获取产品所属的所有类别的名称,并且效果很好。 I get the results I expect. 我得到了我期望的结果。

The problem lies when I filter for one category. 问题出在我筛选一个类别时。 Then the GROUP_CONCAT no longer works, it brings only products from the desired category but not ALL the categories from which that product belongs. 然后,GROUP_CONCAT不再起作用,它仅带来所需类别的产品,而不带来该产品所属的所有类别。

Here's the query when I'm not filtering for a specific category. 这是我未针对特定类别进行过滤时的查询。

SELECT p.*, GROUP_CONCAT( c.title SEPARATOR ', ') as category
FROM products p
INNER JOIN products_categories_relation pcr ON pcr.product_id = p.id
INNER JOIN categories c ON pcr.category_id = c.id
WHERE p.status = 1
GROUP BY p.id
ORDER BY p.name ASC
LIMIT 16
OFFSET 0

And here's the query when I filter for a specific category: 这是我筛选特定类别时的查询:

SELECT p.*, GROUP_CONCAT( c.title SEPARATOR ', ') as categories
FROM product p
INNER JOIN products_categories_relation pcr ON pcr.product_id = p.id
INNER JOIN categories c ON pcr.category_id = c.id
WHERE p.status = 1
AND pcr.category_id = 27 
GROUP BY p.id
ORDER BY p.name ASC
LIMIT 16
OFFSET 0

Can someone help me out? 有人可以帮我吗?

You can try this 你可以试试这个

SELECT p.*, GROUP_CONCAT( c.title SEPARATOR ', ') as categories
FROM product p
INNER JOIN products_categories_relation pcr ON pcr.product_id = p.id
INNER JOIN categories c ON pcr.category_id = c.id
WHERE p.status = 1
AND exists( select * from products_categories_relation where products_categories_relation.category_id = 27 and products_categories_relation.product_id = p.id )
GROUP BY p.id
ORDER BY p.name ASC
LIMIT 16
OFFSET 0

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

相关问题 从产品类别及其所有子类别中获取产品 - Fetch Products from a product category and all its sub-categories 一个产品在多个类别中,从每个类别中查找n个产品-php mysql - one product in many categories, finding n number of products from each category - php mysql mysql 查询产品和该产品的所有类别 - mysql query products and all categories for that product 需要MySQL JOIN才能返回产品所属的每个类别,即使只搜索一个类别 - Need a MySQL JOIN to return all categories a product is in, per product, even if only one category is searched for 从父类别的子类别中选择所有产品 - select all products from child categories in parent category 如何在一个查询中获取值类别和每个类别的计数产品? - How get values categories an count products for each category in one query? MYSQL 8 +:查询查找类别下的产品,当类别在层次结构中并且产品附加到叶类别节点时 - MYSQL 8 + : Query to find the products under categories , when category is in hierarchy and product is attached to leaf category node 如何获得所有类别的产品,但产品A必须来自类别1 - How to get products from all categories but the product A must be from cateogry 1 MySQL筛选器产品类别AND&OR - MySQL Filter Products Category AND & OR 产品过滤器-计算产品 - Product filter - count products
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM