[英]How to match on several rows using left inner join on mysql
I have the following scenario: Not sure how to explain it better but ill try to give some sql details and a valid scenario. 我有以下情况:不确定如何更好地解释它,但生病了尝试给出一些SQL细节和一个有效的方案。
I have a table that have products,one that keeps the product tags, and one that saves the ones added to a product. 我有一个包含产品的表,一个保留产品标签的表,另一个保存添加到产品中的产品。 I can add several tags to one product. 我可以为一个产品添加几个标签。
CREATE TABLE product(
id INT auto_increment primary key,
name varchar(255)
);
CREATE TABLE tags(
id INT auto_increment primary key,
tag_name varchar(255)
);
CREATE TABLE product_tag(
id INT auto_increment primary key,
tag_id INT,
product_id INT
);
The sql query using inner join gives me: 使用内连接的sql查询给了我:
select
product.name,
tags.tag_name
from product
INNER JOIN product_tag ON product.id = product_tag.product_id
INNER JOIN tags ON product_tag.tag_id = tags.id
The result gives me: 结果给了我:
Product 1 | Tag1
Product 1 | Tag2
Product 1 | Tag3
Product 2 | Tag1
Product 2 | Tag3
I want to make a query that check if product 1 have tag1,tag2 and tag3 have all 3 tags attached. 我想创建一个查询,检查产品1是否有tag1,tag2和tag3是否附加了所有3个标签。 Imagine the user on front end selects all tree tags, then i want to show the product that have all tree tags refered to it. 想象一下前端用户选择所有树标签,然后我想显示所有树标签都指向它的产品。 But i get 3 rows and each row contain only one row in this example. 但是我得到3行,在这个例子中每行只包含一行。 What would be the proper way to do this. 什么是正确的方法来做到这一点。
Here's the full query: 这是完整的查询:
SELECT
P.id,
P.name
FROM product_tag PT
INNER JOIN tags T ON PT.tag_id = T.id
INNER JOIN product P ON PT.product_id = P.id
WHERE T.tag_name IN ('Tag1','Tag2','Tag3')
GROUP BY PT.product_id
HAVING COUNT(T.id) = 3
This query will give output only those product id and names which are involved in all the three tags given. 此查询将仅输出在给定的所有三个标记中涉及的那些产品ID和名称。
Explanation: 说明:
Only IN
doesn't guarantee that a single product holds all the three tags given. 只有IN
不能保证单个产品保留给定的所有三个标签。
Since you want result for each product
that's why GROUP BY product.id
comes into play. 因为你想要每个product
的结果,这就是GROUP BY product.id
发挥作用的原因。
Later HAVING COUNT(T.id) = 3
is used to filter out those products only which hold all the three tags given. 之后HAVING COUNT(T.id) = 3
用于过滤掉那些只包含所有三个标签的产品。
so you want to count how many of the chosen tags your product is linked to 因此,您需要计算产品所链接的选定标签的数量
SELECT p.id, count(p.id) as tag_count FROM product p INNER JOIN product_tag pt ON p.id = pt.product_id WHERE pt.tag_id IN (1,2,3) GROUP BY p.id HAVING tag_count = 3;
this will give you ids of products that have exactly 3 tags linked, where ids of those tags are 1,2,3. 这将为您提供正好链接3个标签的产品ID,其中这些标签的ID为1,2,3。 You can replace 1,2,3 with another select or a variable depending on your situation. 您可以根据您的具体情况将1,2,3替换为另一个选择或变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.