简体   繁体   中英

if condition in where clause of mysql query generating dynamically

Hi i am trying to write a query with if in where clause please suggest how can i do it my where condition is as follow

 IF(`page_type` ==1,
    `pages NOT LIKE '%ads/indian-institute-of-technology-bombay.html%' 
  OR pages NOT LIKE '%ads/*%' OR pages NOT LIKE '%indian-institute-of-technology-bombay.html/*%'` ,
   ` pages LIKE '%ads/indian-institute-of-technology-bombay.html%'
  OR pages LIKE '%ads/*%' OR
     pages LIKE '%indian-institute-of-technology-bombay.html/*%'` )

Full Query is

SELECT  *
    FROM  `app_slides`
    WHERE  IF(`page_type` ==1, `pages NOT LIKE '%ads/indian-institute-of-technology-bombay.html%'
              OR  pages NOT LIKE '%ads/*%'
              OR  pages NOT LIKE '%indian-institute-of-technology-bombay.html/*%'` ,
                ` pages LIKE '%ads/indian-institute-of-technology-bombay.html%'
              OR  pages LIKE '%ads/*%'
              OR  pages LIKE '%indian-institute-of-technology-bombay.html/*%'` 
             )

IF works like this:

IF(<condition>, <value if true>, <value if false>)

So as an example

SELECT IF( 'a' = 'a', 1, 0 ); //will return 1
SELECT IF( 'a' = 'b', 1, 0 );//will return 0

Using IF in a WHERE query The following example shows how to use IF in a WHERE query.

SELECT ...
WHERE ...
AND IF(myfield = 'somevalue', 1, 0) = 1

This should not be done in an if, but should be expressed using the logical operators.

select ... from ... where (`page_type` == 1 and ... )
or (`page_type` <>1 and ...)

you may optimize it by rewriting as a union instead of using or in the where criteria.

You can use case for this. Case statement work like this.

SELECT empno, ename, job
  FROM scott.emp
 WHERE (CASE WHEN job = 'MANAGER' THEN '1'  
         WHEN job = 'CLERK'   THEN '2' 
         ELSE '0'  END) IN (1, 2)

You have one or two bugs that the other answers do not address...

   x NOT LIKE 'a'
OR x NOT LIKE 'b'

is always TRUE. Hence, the first part of the IF will always succeed. Perhaps you want

NOT (   x LIKE 'a'
     OR x LIKE 'b' )

specifically,

NOT (   `pages LIKE '%ads/indian-institute-of-technology-bombay.html%' 
      OR pages LIKE '%ads/*%'
      OR pages LIKE '%indian-institute-of-technology-bombay.html/*%'` )

The other possible bug:

ads/*

Is that actually what you are expecting? Note that * , when used in LIKE is not the same as when used in RLIKE .

It does not really matter whether you use IF , CASE , or page_type == 1 and (...) -- each will perform approximately equally. And each will come up with the desired results (after fixing the bug(s) I pointed out). And each will be slow -- that is, each will do a full table scan. This is for 2 reasons:

  • Leading wild cards ( LIKE '%...' ) does not optimize well.
  • OR does not optimize well. (And changing to UNION is useful only if you get rid of all the ORs and the leading wildcards).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM