1

I am working on selecting data according to 3 basic condition which is not the issue,the issue is that if 1st condition satisfied then it should not consider the rest two conditions just like if else but if the 1st condition is false then it goes to 2nd only not to 3rd one just like if elseif and if first two conditions not satisfied then it will consider the 3rd only.

But in mysql when i use OR & AND then it is considering all 3 conditions at a time.

Like this:-

SELECT * FROM `table` WHERE 'main-condition' and ('1st condition' or '2nd condition' or '3rd condition');

After this i also tried with IF in where clause but it also give the same result like OR.

Code:-

SELECT * FROM `table` WHERE 'main-condition' and (IF('1st condition',true,IF('2nd condition',true,IF('3rd condition',true,false))));

Actual Query:-

SELECT 'filter','car-name' FROM `car` WHERE (((`id` = ? and `type` = 'car')and(IF((find_in_set('new car',`filter`) > 0),true,IF((find_in_set('new',`filter`) > 0, true,if((`filter`=""),true,false))))));

It give output

enter image description here

But i need output like this

enter image description here

If any one knows how to handle this type of query then please answer me.

Thanks

Bibhudatta Sahoo
  • 4,808
  • 2
  • 27
  • 51

2 Answers2

1

To do so you need to iterate to all records present in your Database with different conditions. So better is to fetch all records with all possibilities and then filter using PHP script.

Shams Reza
  • 1,087
  • 8
  • 10
1

I have research on this for 2-3 days then only i got that we can't get the desired output as i mentioned above because mysql queries execute with respect to table rows separately not with the whole table.

But i solved this by using mysql Group_concat()

Query:-

SELECT Group_concat( CASE WHEN (find_in_set('new car',`filter`) > 0) THEN car-name END separator ',') exactCars,Group_concat( CASE WHEN (find_in_set('new',`filter`) > 0) THEN car-name END separator ',') possibleCars,Group_concat( CASE WHEN (`filter`="") THEN car-name END separator ',') defaultCars, FROM `car` WHERE (`id` = ? and `type` = 'car');

This provide output like this

enter image description here

Now just using php Explode() with the result then get the desired data like

$exactCars=explode(',',$reult->exactCars);

It give output as

enter image description here

Bibhudatta Sahoo
  • 4,808
  • 2
  • 27
  • 51