简体   繁体   English

mysql查询的where子句中的if条件动态生成

[英]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在where子句中写一个查询,请提出我该如何处理我的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/*%'` )

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工作方式如下:

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. WHERE查询中使用IF以下示例显示如何IF in a WHERE查询中使用IF in a WHERE

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. 这不应在if中完成,而应使用逻辑运算符表示。

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. 您可以通过改写为并集而不是在where条件中使用或来优化它。

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. 始终为TRUE。 Hence, the first part of the IF will always succeed. 因此, IF的第一部分将永远成功。 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 . 请注意, * (在LIKE中使用)与在RLIKE使用时不同。

It does not really matter whether you use IF , CASE , or page_type == 1 and (...) -- each will perform approximately equally. 不管使用IFCASE还是page_type == 1 and (...)性能大致相同。 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. 前导通配符(如LIKE '%...' )的优化效果不佳。
  • OR does not optimize well. OR优化效果不佳。 (And changing to UNION is useful only if you get rid of all the ORs and the leading wildcards). (并且仅当您除去所有ORs 前导通配符时,更改为UNION才有用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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