hasan movahed hasan movahed -4 years ago 148
MySQL Question

how can use two where in one column with mysql?

im using mysql for database project , i have one mysql table taxonomy .

i needed post_id with condition tax_id = 1 and tax_id = 2 in mysql , also if i want usable for 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

for example my using this sql but not working :

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

Answer Source

Here's one option using 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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download