我敢打赌这是一个简单的问题,但我的 MySQL 印章有点生疏了。需要一点帮助来为以下使用多条件连接的场景生成正确的查询。我有一个属性表(想想标签)和另一个表,其中有一个记录列表。当我想匹配在查询中定义了多个属性的页面时遇到问题。
+--------------------------------------+
| ATTRIBUTES |
+-----------+------------+-------------+
| name | value | page_id |
+-----------+------------+-------------+
| type food 2 |
| fruit apple 2 |
| color green 2 |
| type frog 3 |
| color green 3 |
+--------------------------------------+
+--------------------------------------+
| PAGES |
+--------------+-----------------------+
| page_id | title |
+--------------+-----------------------+
| 2 Granny Smith |
| 3 Kermit the Frog |
+--------------------------------------+
用例
Query: type+food
Results: Granny Smith
Query: color+green
Results: Granny Smith, Kermit the Frog
Query: type+food and color+green
Results: Granny Smith
这是我正在玩的查询,但显然不起作用:
SELECT page.page_id, page.title
FROM page_attributes
LEFT JOIN page ON (page_attributes.page_id = page.page_id)
WHERE (page_attributes.name = 'color'
AND page_attributes.value = 'green')
AND (page_attributes.name = 'type'
AND page_attributes.value = 'food')