我正在尝试按类别过滤结果。 所以从下面的ItemKeywords表中
SELECT prod_id FROM ItemKeywords
WHERE (keyword_id=96 OR keyword_id=96) /* Metal category */
AND keyword_id=55 /* Center category */
WHERE (Rose Gold OR White Gold)
AND Princess Cut
这应该只上调100657和102163
玫瑰金或白金
但需要成为Princess Cut
== Keywords ==========================
|keyword_id |KeywordName |category
| 55 | Princess Cut | Center
| 96 | Rose Gold | Metal
| 102 | White Gold | Metal
| 88 | Platinum | Metal
== ItemKeywords ==========================
|keyword_id |prod_id |category
| 96 | 100657 | Metal
| 55 | 100657 | Center
| 102 | 102163 | Metal
| 55 | 102163 | Center
| 88 | 100686 | Metal
| 55 | 100686 | Center
我试过这个
SELECT DISTINCT(p.prod_id), p.header, p.image
FROM tblproducts AS p
JOIN ItemKeywords AS k1 ON k1.prod_id=p.prod_id AND k1.keyword_id=96 OR k1.keyword_id=101
JOIN ItemKeywords AS k2 ON k2.prod_id=p.prod_id AND k2.keyword_id=55
WHERE p.active=1
和这个
SELECT DISTINCT(p.prod_id), p.header, p.image
FROM ItemKeywords AS k JOIN tblproducts AS p ON k.prod_id=p.prod_id
WHERE p.active=1 AND p.retire=0 AND p.category_id=2
AND k.keyword_id=55 AND k.keyword_id=102 OR k.keyword_id=101
等等。我似乎无法弄清楚这一点。
任何见解都会很棒。
由于
答案 0 :(得分:0)
SELECT DISTINCT(p.prod_id), p.header, p.image
FROM tblproducts AS p
JOIN ItemKeywords AS k1 ON k1.prod_id=p.prod_id
JOIN ItemKeywords AS k2 ON k2.prod_id=p.prod_id
WHERE p.active=1 AND p.retire=0 AND p.category_id=2
AND (k1.keyword_id=96 OR k1.keyword_id=102) AND k2.keyword_id=55