[英]Mysql SELECT CASE WHEN something then return field
I have two field nnmu and nnmi , 我有两个字段nnmu和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. 起初一切看起来都不错,但不知何故它混合了值,当nnmi和nnmu都等于0时它起作用,但当任何一个值为1时它返回无意义。 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
You are mixing the 2 different CASE
syntaxes inappropriately. 您正在混合使用2种不同的
CASE
语法。
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. 在MySQL中,这将最终测试
u.nnmu
是否等于布尔表达式u.nnmu ='0'
本身的值。 Regardless of whether u.nnmu
is 1
or 0
the result of the case expression itself will be 1
无论
u.nnmu
是1
还是0
,case表达式本身的结果都是1
For example if nmu = '0'
then ( nnmu ='0'
) evaluates as true
(1) and ( nnmu ='1'
) evaluates as false
(0). 例如,如果
nmu = '0'
则( nnmu ='0'
)评估为true
(1)并且( nnmu ='1'
)评估为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). 如果
nmu = '1'
则( nnmu ='0'
)评估为false
(0)并且( nnmu ='1'
)评估为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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.