Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql SELECT CASE WHEN something then return field

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 
like image 425
supermus Avatar asked Dec 04 '10 23:12

supermus


People also ask

Can we use CASE condition in where clause in SQL?

CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING.

Which is faster case or if in MySQL?

The MySQL CASE statement is faster in comparison to PHP if statement. The PHP if statement takes too much time because it loads data and then process while CASE statement does not.

Can we use nested if in MySQL?

As with other flow-control constructs, IF ... END IF blocks may be nested within other flow-control constructs, including other IF statements. Each IF must be terminated by its own END IF followed by a semicolon.

What is select * from in MySQL?

The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.


1 Answers

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 
like image 88
Martin Smith Avatar answered Sep 24 '22 00:09

Martin Smith