简体   繁体   English

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

[英]how can use two where in one column with mysql join query?

im using mysql for database project , i have one mysql table taxonomy . 即时通讯使用mysql进行数据库项目,我有一个mysql表分类。

i needed post_id with condition tax_id = 1 and tax_id = 2 in mysql , also if i want usable for join sql . 我需要在mysql中使用条件tax_id = 1和tax_id = 2的post_id,如果我想要用于join sql也是如此。

example : 例如:

post_id | tax_id
------- | ------
  1     |   1
  1     |   2
  2     |   1
  2     |   3 

i want postid equal with condation taxid equal 1 and 2 我希望postid等于condation taxid等于1和2

for example my using this sql but not working : 例如我使用这个SQL但不工作:

SELECT * FROM `term_relationships` WHERE tax_id = 9 AND tax_id = 1120 ;

Here's one option using conditional aggregation : 这是使用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

Try this... 尝试这个...

You can use IN clause. 您可以使用IN子句。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM