简体   繁体   English

如果mysql中的条件无法按预期工作

[英]If condition in mysql not working as expected

I am having two columns which are of integer types so I want to select the row if one of them have any data except "NULL" or "0". 我有两列为整数类型,因此如果其中之一具有除“ NULL”或“ 0”以外的任何数据,我想选择该行。

cola     colb    colc    cold
NULL       12     plo     123
12         0      hou     NULL
0          0      plo      0
0          0      hou      123
NULL       13     hou      231
NULL       0      plo      NULL

So i want to select only 1,2,5 records so basically i have to select if colc is plo then any one of cola or colb should have value and if colc is hou then cold should have value. 所以我只想选择1,2,5条记录,所以基本上我必须选择colc是plo,然后cola或colb中的任何一个都应该有值,而如果colc是hou,那么cold应该就可以了。

I have written a query but its not giving proper result 我写了一个查询,但是没有给出正确的结果

I am giving my where condition where 我在给我哪里条件

IF ( cold = 'plo', cold, colc) IS NOT NULL 
AND IF ( cold = 'plo', cold, colc) <> '' 
AND ( ( (IF (colc <> 'plo', cola, colc) IS NOT NULL) 
    AND (IF (colc <> 'plo', cola, colc) <> '' )) 
       OR ( (IF (colc <> 'plo', colb, colc) IS NOT NULL) 
          AND (IF (colc <> 'plo', colb, colc) <> '' )) ) 

this is my where condition but after applying this also i am getting results which have both columns as 0 and colc as 'plo' 这是我的条件,但应用此条件后,我也得到结果,列均为0,colc为“ plo”

Is there something wrong in the where clause where子句有什么问题吗

The predicates look way more complicated than they need to be. 谓词看起来比需要的复杂得多。

I'm not sure what the value of colc has to do with checking if cola or colb contains a non-zero integer value. 我不确定colc的值与检查colacolb包含非零整数值有什么关系。

This would suffice: 这样就足够了:

   WHERE ( t.cola OR t.colb )

If you have some condition you want to check on colc , where that's not equal to 'plo' or 'plot' 如果您有某种状况,请检查colc ,其中不等于'plo''plot'

   WHERE ( t.cola OR t.colb )
     AND t.colc NOT IN ('plo','plot')

There are other expressions which are more complicated and more ANSI-compliant which will achieve an equivalent result. 还有其他更复杂,更符合ANSI要求的表达式,它们将获得同等的结果。

FOLLOWUP 跟进

Assuming the (unfortunately named) columns cola , colb and colc are integer types (or numeric types), you can do something like this: 假设colacolbcolc (不幸的是)列是整数类型(或数字类型),则可以执行以下操作:

  WHERE (t.colc = 'plo' AND (t.cola OR t.colb))
     OR (t.colc = 'hou' AND t.cold) 

The question was edited to clarify the specification... query should return rows that meet either of the following conditions: 已对问题进行了编辑,以澄清规范。查询应返回满足以下任一条件的行:

  • if colc is 'plo' then any one of cola or colb should have value other than 0 or NULL 如果colc为'plo',则colacolb任何一个都应具有非0NULL

  • if colc is 'hou' then cold should have value other than 0 or NULL 如果colc是'hou',那么cold应该具有非0NULL

Let's unpack the WHERE clause a little bit. 让我们对WHERE子句进行一些解压缩。

We like to use parens around all of the boolean AND / OR expressions. 我们喜欢在所有布尔AND / OR表达式周围使用parens。 This makes the order of precedence explicit. 这使得优先顺序明确。 This should help the reader who doesn't know/doesn't remember whether AND or OR has a higher precedence; 这应该可以帮助不知道/不记得ANDOR优先级较高的读者; using parens, even where they aren't strictly necessary, just removes some possible ambiguity, and makes our intent more clear. 即使在并非绝对必要的地方使用parens,也可以消除一些可能的歧义,并使我们的意图更加明确。

In a boolean context, an integer value evaluates to either NULL, FALSE, or TRUE 在布尔上下文中, 整数值的取值为NULL,FALSE或TRUE

  • if the value of the expression is NULL, the boolean evaluation returns NULL. 如果表达式的值为NULL,则布尔评估返回NULL。
  • if the value of the expression is 0 (zero), it evaluates to boolean FALSE 如果表达式的值为0(零),则结果为布尔值FALSE
  • if the value of the expression is other than NULL or 0, it evaluates to boolean TRUE 如果表达式的值不是NULL或0,则结果为布尔值TRUE

The SQL three-valued boolean logic, expression (a OR b) evaluates to TRUE if either a or b evaluates to TRUE. 如果ab计算结果为TRUE,则SQL三值布尔逻辑表达式(a OR b)计算结果为TRUE。

  OR    TRUE   FALSE  NULL
 -----  -----  -----  -----
 TRUE   TRUE   TRUE   TRUE 
 FALSE  TRUE   -      -
 NULL   TRUE   -      - 

With the AND operator, the expression (a AND b) evaluates to TRUE only of both a and b evaluate to TRUE. 使用AND运算符,表达式(a AND b)仅将ab值都计算为TRUE。

rewrite 改写

This same WHERE clause: 相同的WHERE子句:

  WHERE (t.colc = 'plo' AND (t.cola OR t.colb))
     OR (t.colc = 'hou' AND t.cold) 

could be re-written to slightly more verbose: 可以重写为稍微冗长一些:

  WHERE (t.colc = 'plo' AND ((t.cola<>0) OR (t.colb<>0)))
     OR (t.colc = 'hou' AND t.cold<>0)

or we could re-write that to be more convoluted, but it's not really necessary to do that... it would just make it harder to decipher. 或我们可以重写它以使其更令人费解,但实际上并不需要这样做……这只会使解密变得更加困难。

NOT(((cola IS NULL) OR (cola=0)) AND ((colb IS NULL) OR (colb=0))) AND colc<>'plo'

让你2,4 - 约COLC是巴解组织在您的定义不是钻头,而是在代码

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

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