[英]Join one to many with multiple where
This is products table: 这是产品表:
+-----------+
| id | name |
+-----------+
| 1 | A |
+-----------+
| 2 | B |
+-----------+
| 3 | C |
+-----------+
This is product_filter table: 这是product_filter表:
+-----------------+-----------+
| id | product_id | filter_id |
+-----------------+-----------+
| 1 | 1 | 1 |
+-----------------------------+
| 2 | 1 | 2 |
+-----------------------------+
| 3 | 2 | 1 |
+-----------------------------+
| 4 | 2 | 2 |
+-----------------------------+
| 5 | 2 | 3 |
+-----------------------------+
| 6 | 3 | 1 |
+-----------------------------+
| 7 | 3 | 3 |
+-----------------------------+
i want if where product_filter.filter_id = 1 and product_filter.filter_id = 2 the result of product.id is 1 and 2 我想如果product_filter.filter_id = 1和product_filter.filter_id = 2的地方product.id的结果是1和2
if where product_filter.filter_id = 1 and product_filter.filter_id = 3 the result of product.id is 1 and 3 如果product_filter.filter_id = 1且product_filter.filter_id = 3,则product.id的结果为1和3
This is my query, and it's not works: 这是我的查询,它不起作用:
SELECT `products`.`id`, `product_filter`.`filter_id`
FROM `products`
JOIN `product_filter` ON `product_filter`.`product_id` = `products`.`id`
WHERE `product_filter`.`filter_id` = '1' AND `product_filter`.`filter_id` = '2'
Instead of AND
use IN
: 代替
AND
使用IN
:
SELECT
p.id,
pf.filter_id
FROM products p
INNER JOIN product_filter pf
ON pf.product_id = p.id
WHERE
pf.filter_id IN (1, 2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.