Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL query with multiple where clauses

Tags:

mysql

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?

like image 798
Shameer Rahman Avatar asked Sep 17 '25 00:09

Shameer Rahman


1 Answers

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')
like image 101
while1 Avatar answered Sep 19 '25 15:09

while1