Shameer Rahman
Shameer Rahman

Reputation: 131

MySQL query with multiple where clauses

I have a table wp_postmeta with columns called meta_key and meta_value. I have 5 records in meta_key (location,area,price,bedrooms,bathrooms)

screenshot of table here

For example, I want to find a hotel in Texas with 2 bathrooms:

select post_id from wp_postmeta where meta_key = 'location' and meta_value = 'texas' and where meta_key = 'bathrooms' and meta_value= '2';

I know the above SQL command is not valid. Can anyone please help me to achieve the above result?

Upvotes: 4

Views: 23712

Answers (1)

while1
while1

Reputation: 3370

You can try mysql subquery:

select post_id 
from wp_postmeta 
where meta_key = 'location' and meta_value = 'texas' 
                  and post_id IN (select post_id 
                  from wp_postmeta 
                  where meta_key = 'bathrooms' and meta_value= '2')

Upvotes: 4

Related Questions