[sql] 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

This question is related to sql select mysql case

The answer is


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

Examples related to sql

Passing multiple values for same variable in stored procedure SQL permissions for roles Generic XSLT Search and Replace template Access And/Or exclusions Pyspark: Filter dataframe based on multiple conditions Subtracting 1 day from a timestamp date PYODBC--Data source name not found and no default driver specified select rows in sql with latest date for each ID repeated multiple times ALTER TABLE DROP COLUMN failed because one or more objects access this column Create Local SQL Server database

Examples related to select

Warning: Use the 'defaultValue' or 'value' props on <select> instead of setting 'selected' on <option> SQL query to check if a name begins and ends with a vowel Angular2 *ngFor in select list, set active based on string from object SQL: Two select statements in one query How to get selected value of a dropdown menu in ReactJS DATEDIFF function in Oracle How to filter an array of objects based on values in an inner array with jq? Select unique values with 'select' function in 'dplyr' library how to set select element as readonly ('disabled' doesnt pass select value on server) Trying to use INNER JOIN and GROUP BY SQL with SUM Function, Not Working

Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to case

PostgreSQL CASE ... END with multiple conditions SQL Server: use CASE with LIKE SQL Server IIF vs CASE SELECT using 'CASE' in SQL SELECT query with CASE condition and SUM() GROUP BY + CASE statement SQL use CASE statement in WHERE IN clause SQL Server - Case Statement where to place CASE WHEN column IS NULL in this query Regarding Java switch statements - using return and omitting breaks in each case