php - Mysql Query required with one to many relation for searching with matching -
first table
property
id name 1 abc 2 xyz 3 ghq
property_options
id property_id alternative 1 1 terrace 2 1 balcony 3 1 garaj 1 2 terrace 2 2 balcony 3 2 garaj
i want filter property 3 options (terrace
, balcony
, garaj
)
if user check 3 options property come have 3 options not 2 or one.
i using aggregation , group by
:
select p.propertyid property p grouping p.propertyid having sum(property_option = 'terrace') > 0 , sum(property_option = 'balcony') > 0 , sum(property_option = 'garaj') > 0;
each of conditions counts number of times property appears. flexible method. if wanted first two, not "garaj", use:
having sum(property_option = 'terrace') > 0 , sum(property_option = 'balcony') > 0 , sum(property_option = 'garaj') = 0;
php mysql
No comments:
Post a Comment