如何使用mysql连接查询在一列中使用两个位置?

时间:2016-09-01 03:58:22

标签: mysql sql

我正在使用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 ;

2 个答案:

答案 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)

试试这个......

您可以使用IN子句。

SELECT * FROM `term_relationships` WHERE term_taxonomy_id in (9,1120);