0

我有以下表格..

items

ID | Title   |       Desc
67 | Title_1 |  Description_1
68 | Title_2 |  Description_2 

tag

ID |  Name
5  |  tag_1
7  | tag_2
22 | tag_3
23 | tag_4 

tag_ref

ID  | tagID   | itemID
1   | 5       | 68
2   | 7       | 67
3   | 5       | 67
4   | 23      | 68
5   | 22      | 68 

现在,我想选择所有具有相同 tagID 且当前产品项目 id = 67 的项目

因为我想通过标签获取相关项目。

4

2 回答 2

0
SELECT * FROM items JOIN item tag_ref on tag_ref.itemID = items.ID JOIN tag 
on tag.ID = tag_ref.tagID where item.ID = 67
于 2013-10-02T09:31:31.777 回答
0

加入itemsand tag_ref,仅返回子查询中存在 tagID 的项目,该子查询返回项目 67 的所有 tagID:

SELECT items.*
FROM items
JOIN tag_ref as t on t.itemID = items.ID
WHERE t.tagID in (SELECT t2.tagID 
                  FROM tag_ref as t2
                  JOIN items as i2 on i2.ID = t2.itemID
                  WHERE i2.ID = 67)
于 2013-10-02T09:31:38.313 回答