我试图在过去几天解决这个问题,我们将不胜感激。
我有一个Return项目,在taxonomy1中有term1,在taksonomy2中有term2。
这是我的疑问:
SELECT items.*
FROM (`items`)
LEFT JOIN `rel_taxonomy` ON `rel_taxonomy`.`item_id` = `items`.`id`
LEFT JOIN `terms_taxonomy` ON `terms_taxonomy`.`terms_taxonomy_id` = `rel_taxonomy`.`terms_taxonomy_id`
LEFT JOIN `terms` ON `term`.`id` = `terms_taxonomy`.`term_id`
WHERE (items.org = '2') AND ( ((terms_taxonomy.taxonomy = '1') AND (term.pojam LIKE '%term1%')) AND ((terms_taxonomy.taxonomy = '2') AND (term.pojam LIKE '%term2%')))
GROUP BY `items`.`id`
如果我更换线路:
WHERE (items.org = '2') AND ( ((terms_taxonomy.taxonomy = '1') AND (term.pojam LIKE '%term1%')) AND ((terms_taxonomy.taxonomy = '2') AND (term.pojam LIKE '%term2%')))
使用以下内容(用OR替换AND):
WHERE (items.org = '2') AND ( ((terms_taxonomy.taxonomy = '1') AND (term.pojam LIKE '%term1%')) OR((terms_taxonomy.taxonomy = '2') AND (term.pojam LIKE '%term2%')))
一切正常。但我仍然需要和。
表格方案
items
id | title | org
rel_taxonomy
item_id | terms_taxonomy_id
terms_taxonomy
terms_taxonomy_id | terms_id | taxsonomy
terms
id | term
所以我试图在takonomyomy2中获得在taxonomy1和term2中有term1的项目。
答案 0 :(得分:0)
使用OR
并添加HAVING
子句
SELECT i.id
FROM items i
LEFT JOIN rel_taxonomy rt ON rt.item_id = i.id
LEFT JOIN terms_taxonomy tt ON tt.terms_taxonomy_id = rt.terms_taxonomy_id
LEFT JOIN terms t ON t.id = tt.term_id
WHERE i.org = '2'
AND
(
(tt.taxonomy = '1' AND t.pojam LIKE '%term1%')
OR (tt.taxonomy = '2' AND t.pojam LIKE '%term2%')
)
GROUP BY i.id
having count(distinct tt.taxonomy) = 2