MySQL Select - 连接匹配所有表的表

时间:2017-11-07 01:34:44

标签: php mysql

我有一个包含3个表的数据库 - 产品,标签和& product_tags。我需要一个查询 返回包含所有指定标记的所有产品。换句话说,不要回来 产品,如果它没有所有指定的标签。

products
+----+-----+--------+------------------------+
| id | uid | name   | description            |
+----+-----+--------+------------------------+
| 1  | p1  | ball   | something that bounces |
+----+-----+--------+------------------------+
| 2  | p2  | block  | for building stuff     |
+----+-----+--------+------------------------+
| 3  | p3  | bucket | holds stuff            |
+----+-----+--------+------------------------+
| 4  | p4  | shovel | scoops stuff           |
+----+-----+--------+------------------------+


tags
+----+-----+--------+
| id | uid | name   |
+----+-----+--------+
| 1  | t1  | blue   |
+----+-----+--------+
| 2  | t2  | red    |
+----+-----+--------+
| 3  | t3  | green  |
+----+-----+--------+
| 4  | t4  | yellow |
+----+-----+--------+
| 5  | t5  | orange |
+----+-----+--------+


product_tags
+-------------+---------+
| product_uid | tag_uid |
+-------------+---------+
| p1          | t1      |
+-------------+---------+
| p1          | t2      |
+-------------+---------+
| p2          | t3      |
+-------------+---------+
| p2          | t4      |
+-------------+---------+
| p2          | t5      |
+-------------+---------+
| p3          | t1      |
+-------------+---------+
| p4          | t1      |
+-------------+---------+
| p4          | t5      |
+-------------+---------+
  

以下是我正在寻找的一些结果示例:

选择红色的产品(t2):

+-------------+------+
| product_uid | name |
+-------------+------+
| p1          | ball |
+-------------+------+

选择所有红色&产品蓝色(t2,t1):

+-------------+------+
| product_uid | name |
+-------------+------+
| p1          | ball |
+-------------+------+

选择所有红色,蓝色和黄色(t2,t1,t4):

+--------------------+
|     NO PRODUCTS    |
+--------------------+

选择所有蓝色产品(t1):

+-------------+--------+
| product_uid | name   |
+-------------+--------+
| p1          | ball   |
+-------------+--------+
| p3          | bucket |
+-------------+--------+
| p4          | shovel |
+-------------+--------+

选择所有蓝色和蓝色的产品橙色(t1,t5):

+-------------+--------+
| product_uid | name   |
+-------------+--------+
| p4          | shovel |
+-------------+--------+
  

这是一个已经设置的SQLFiddle的链接。我尝试了LEFT JOIN,但它并没有让我得到我正在寻找的东西。

http://sqlfiddle.com/#!9/795615/6

2 个答案:

答案 0 :(得分:2)

编辑:更新了@ spencer7593的建议

假设您的代码是唯一的,您可以使用count来仅过滤具有等于传递的代码的匹配数的产品。

这种方法的优点是允许无限制的标签作为输入。

SELECT products.uid
FROM product_tags
JOIN products ON product_tags.product_uid = products.uid
WHERE product_tags.tag_uid IN ('t2', 't1')
GROUP BY products.uid
HAVING COUNT(DISTINCT product_tags.tag_uid) = 2

或3个标签:

SELECT products.uid
FROM product_tags
JOIN products ON product_tags.product_uid = products.uid
WHERE product_tags.tag_uid IN ('t3', 't2', 't1')
GROUP BY products.uid
HAVING COUNT(DISTINCT product_tags.tag_uid) = 3

依旧......

答案 1 :(得分:0)

试试这个:

select product_id,tag_uuid,uid,d.name,description,color from
(

select product_id,tag_uuid,b.name as color from
(select product_id,tag_uuid from product_tags) as a
LEFT JOIN
(select uid,name from tags) as b
on a.tag_uuid = b.uid

) as c
left JOIN
(select id,uid,name,description from products) as d
on c.product_id = d.uid


where color = 'blue'

如果您需要其他颜色,只需更改位置即可。