【发布时间】:2021-10-20 02:26:05
【问题描述】:
如何选择属于两个类别的产品并删除一个类别?
示例:我希望只选择属于第一类和第二类的产品,不包括第三类。
产品必须同时具有第一类和第二类。
如果产品有类别一、二和三,则排除。
我试图做这样的事情,但它不起作用:
SELECT products.product_id , products.product_name FROM products
INNER JOIN product_category_relations ON product_category_relations.relations_product_id = products.product_id
WHERE relations_category_id IN (1,2) AND relations_category_id NOT IN (3)
GROUP BY products.product_id
已选择产品 ID:1 和 2。
示例产品表
| product_id | product_name |
|---|---|
| 1 | tshirt |
| 2 | pants |
| 3 | Bikini |
| 4 | Jumper |
类别表示例
| category_id | category_name |
|---|---|
| 1 | category one |
| 2 | category two |
| 3 | category three |
| 4 | category four |
透视 product_category_relations 表
| relations_category_id | relations_product_id |
|---|---|
| 1 | 1 |
| 2 | 1 |
| 4 | 1 |
| 1 | 2 |
| 2 | 2 |
| 1 | 3 |
| 2 | 3 |
| 3 | 3 |
| 1 | 4 |
| 4 | 4 |
【问题讨论】:
标签: mysql sql group-by count having