I will post only the part of the sql statament that contains WHERE
clause:
//Together
WHERE `energy_extra`.`PacketFk` IN('7')
OR `energy_extra`.`house_extra_id` IN('4')
//Not together (separate where statements)
AND `People_from` <= '4'
AND `People_to` >= '4'
AND `Size` >= '100'
AND `Size` <= '200'
AND `houseconstructiontypelist`.`ConstructionTypeFk` IN('1', '2')
I can't change the order of these lines because they are a part of a much larger query written in codeigniter active record with conditional statements. What I can change is the SQL itself.
I don't get the desired result because WHERE foo IN ('7')
OR bar IN ('4')
is followed by AND
thus SQL thinks those further conditions are a part of the OR bar IN()
which they are not. They need to start with a new WHERE
statement and every line after that can start with AND
again.
I tried this but it doesn't work:
//Together
WHERE `energy_extra`.`PacketFk` IN('7')
OR `energy_extra`.`house_extra_id` IN('4')
//Not together (separate where statements)
WHERE `People_from` <= '4'
AND `People_to` >= '4'
AND `Size` >= '100'
AND `Size` <= '200'
AND `houseconstructiontypelist`.`ConstructionTypeFk` IN('1', '2')
Logic is as follows:
find rows where energy_extra.PacketFk is equal to 7 or where energy_extra.house_extra_id is equal to 4
then check all the other conditions