我正在使用mysql进行数据库项目,我有一个 mysql 表分类。
我需要在mysql中使用条件tax_id = 1和tax_id = 2的post_id,如果我想可用于加入 sql。
示例:
post_id | tax_id
------- | ------
1 | 1
1 | 2
2 | 1
2 | 3
我希望postid等于condation taxid等于1和2
例如我使用这个sql但没有工作:
SELECT * FROM `term_relationships` WHERE tax_id = 9 AND tax_id = 1120 ;
答案 0 :(得分:2)
以下是使用conditional aggregation
的一个选项:
select post_id
from term_relationships
group by post_id
having count(case when term_taxonomy_id in (9, 1120) then 1 end) = 2
答案 1 :(得分:1)