Skip to content
Advertisement

Mysql SELECT CASE WHEN something then return field

I have two field nnmu and nnmi ,

and reverse,

At first everything looks good, but somehow it mix up values, it work when nnmi and nnmu both are equal to 0, but when either value is 1 it returns nonsense. Any help?

Advertisement

Answer

You are mixing the 2 different CASE syntaxes inappropriately.

Use this style (Searched)

Or this style (Simple)

Not This (Simple but with boolean search predicates)

In MySQL this will end up testing whether u.nnmu is equal to the value of the boolean expression u.nnmu ='0' itself. Regardless of whether u.nnmu is 1 or 0 the result of the case expression itself will be 1

For example if nmu = '0' then (nnmu ='0') evaluates as true (1) and (nnmu ='1') evaluates as false (0). Substituting these into the case expression gives

if nmu = '1' then (nnmu ='0') evaluates as false (0) and (nnmu ='1') evaluates as true (1). Substituting these into the case expression gives

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement