Force mysql not to return a result on bit_or

I have a mysql query that uses bit_or in the result set. When there is no result set, the bit_or function returns 0 (this is correct according to the mysql documentation).

For example:

select n from t1 where a=1;

      

Returns no rows.

select bit_or(n) from t1 where a=1;

      

Returns 0

I need to force the last request to return the result from the bit_or request instead, eg. (in pseudocode, because I don't know the correct call):

select bit_or(n) unless 0 from t1 where a=1;

      

Does anyone know how I can do this? (To clarify, I need to return a "no result at 0" value for an external program, which unfortunately cannot be changed).

+2


source to share


2 answers


You can use the case

for null

from string operator , for example:

select
    case 
        when bit_or(n) = 0 then null
        else bit_or(n)
    end
from
    t1
where
    a=1

      

Or, if you want to ignore the line entirely, you can do this in a sentence having

:



select
    bit_or(n)
from
    t1
where
    a=1
having
    bit_or(n) <> 0

      

The clause is having

used essentially for where

for aggregate columns (for example, sum

or max

). In this case, your total column is bit_or

. If you have additional columns in your result set, you will need to include them in the statement group by

after the sentence where

, but before the sentence having

.

The first example will return null

for this string, and the second example will not return a string if it bit_or(n)

is 0.

+3


source


SELECT *
FROM (
    SELECT BIT_OR(`n`) AS `bit`
    FROM `t1`
    WHERE `a` = 1
) AS `bit`
WHERE `bit`

      



0


source







All Articles