MySQL and generating where claused -


i doing work mysql , have unique situation solution comes me have every possible combination defined. wondering if there easier/better way of doing this.

this sql creates 5 categories of mpgs

select count(if(a.mpgcombined < 18, a.mpgcombined, null)) under18, count(if(a.mpgcombined >= 18 , a.mpgcombined <= 23, a.mpgcombined, null)) under24, count(if(a.mpgcombined > 23 , a.mpgcombined <= 28, a.mpgcombined, null)) under29, count(if(a.mpgcombined > 28 , a.mpgcombined <= 35, a.mpgcombined, null)) under36, count(if(a.mpgcombined > 35, a.mpgcombined, null)) over35 styles a, jpgs b, models c, divisions d d.divisionid = c.divisionid , a.styleid = b.styleid , c.modelid = a.modelid , a.mktclassid in ($this->mktclassids) $this->where , a.sequence = 0; 

these categories , there counts shown user bunch of checkboxes, if count > 0. user can select many category ranges want. problem came across if user selects under18 , under24. clause not work:

and a.mpgcombined < 18 , a.mpgcombined >= 18 , a.mpgcombined <= 23 

this obviously:

and a.mpgcombined <= 23 

this stuff dynamic not know conditions be, first option allows me not have define sql each possible scenario. there anyway around not having define sql every possible combination? sql not strong point.

if thinking query make no sense clause, not worry sql not called if user selects mpg, gets disabled, clause gets applied several other querys.

i need know how dynamically create clause given ranges, preferably without having define clause every possible situation, ie:

if(under18 && under24)     $where = 'and a.mpgcombined <= 23'; elseif (under29 && under18....) 

you can picture of im thinking.

thanks

when generating where clause wrap each condition , join them or.

so example becomes:

and ((a.mpgcombined < 18 , a.mpgcombined >= 18) or (a.mpgcombined <= 23)) 

Comments