Mysql SELECT CASE WHEN something then return field

supermus picture supermus · Dec 5, 2010 · Viewed 134.7k times · Source

I have two field nnmu and nnmi ,

if nnmu is equal to 1, I need to return naziv_mesta from **mesto_istovara**,
else if it's =0 I need to return naziv_mesta from mesto_utovara table

and reverse,

if nnmi is equal to 1, then I need  to return naziv_mesta from **mesto_utovara,** 
else if it's =0 need to return naziv_mesta from mesto_istovara.

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?

select u.id_utovar,
             u.datum_isporuke,
             u.broj_otpremnice,
             r.naziv_robe,              
             CASE u.nnmu 
              WHEN u.nnmu ='0' THEN mu.naziv_mesta
              WHEN u.nnmu ='1' THEN m.naziv_mesta
             ELSE 'GRESKA'
             END as mesto_utovara,
             CASE u.nnmi
              WHEN u.nnmi = '0' THEN m.naziv_mesta 
              WHEN u.nnmi = '1' THEN mu.naziv_mesta
              ELSE 'GRESKA'
             END as mesto_istovara,                                                
             m.adresa,
             m.kontakt_osoba,
             m.br_telefona,
             u.broj_paleta,
             u.bruto,
             k.username,
             u.napomena,                  
             v.registracija,
             p.naziv_prevoznika,
             u.cena,
             u.korisnik_logistika,
             u.korisnik_analitika,
             u.datum_unosa,
             u.vreme_unosa,
             u.zakljucan,
             u.id_mesto_utovara,
             u.id_mesto_istovara,
             u.nnmu,
             u.nnmi             
      FROM utovar u ,mesto_utovara mu, mesto_istovara m, roba r, vozila v,prevoznik p, korisnik k
      WHERE u.id_mesto_istovara=m.id_mesto_istovara
       and k.id_korisnik = u.korisnik
       and r.id_robe=u.id_robe 
       and u.id_mesto_utovara = mu.id_mesto_utovara 
       and v.id_vozilo = u.id_vozilo 
       and p.id_prevoznik = u.id_prevoznik
       ORDER by u.id_utovar DESC

Answer

Martin Smith picture Martin Smith · Dec 5, 2010

You are mixing the 2 different CASE syntaxes inappropriately.

Use this style (Searched)

  CASE  
  WHEN u.nnmu ='0' THEN mu.naziv_mesta
  WHEN u.nnmu ='1' THEN m.naziv_mesta
 ELSE 'GRESKA'
 END as mesto_utovara,

Or this style (Simple)

  CASE u.nnmu 
  WHEN '0' THEN mu.naziv_mesta
  WHEN '1' THEN m.naziv_mesta
 ELSE 'GRESKA'
 END as mesto_utovara,

Not This (Simple but with boolean search predicates)

  CASE u.nnmu 
  WHEN u.nnmu ='0' THEN mu.naziv_mesta
  WHEN u.nnmu ='1' THEN m.naziv_mesta
 ELSE 'GRESKA'
 END as mesto_utovara,

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

 SELECT CASE  '0'
  WHEN 1 THEN '0'
  WHEN 0 THEN '1'
 ELSE 'GRESKA'
 END as mesto_utovara

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

 SELECT CASE  '1'
  WHEN 0 THEN '0'
  WHEN 1 THEN '1'
 ELSE 'GRESKA'
 END as mesto_utovara