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".
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.
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'
Is there something wrong in the where clause
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.
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'
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.
FOLLOWUP
Assuming the (unfortunately named) columns cola
, colb
and colc
are integer types (or numeric types), you can do something like this:
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
if colc
is 'hou' then cold
should have value other than 0
or NULL
Let's unpack the WHERE clause a little bit.
We like to use parens around all of the boolean AND
/ OR
expressions. 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; using parens, even where they aren't strictly necessary, just removes some possible ambiguity, and makes our intent more clear.
In a boolean context, an integer value evaluates to either NULL, FALSE, or TRUE
The SQL three-valued boolean logic, expression (a OR b)
evaluates to TRUE if either a
or b
evaluates to 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.
rewrite
This same WHERE clause:
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是巴解组织在您的定义不是钻头,而是在代码
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.