簡體   English   中英

MYSQL 嘗試根據條件獲取值時出錯的情況

[英]MYSQL case when error when tried to get value based on condition

為什么我收到此查詢錯誤? 有什么我想念的嗎?

因此,如果“via”字段為 O,我想獲取 online_date,那么如果“via”字段為 MI,我想獲取 manual_date

SELECT a.seqreq,
b.idfieldb ,
c.idfieldc ,
a.number_given,
case upper(a.via) when 'O' then 'Online'
when 'O' then 'Online'
when 'M' then 'Manual'
else a.via
end as via,
case when upper(a.via) when 'O' then a.online_date
when 'M' then a.manual_date //it says error on this line ?
else a.others_date
end as date_registered
from mytablea a 
left join tbl_b b on a.idfielda=b.idfieldb
left join tbl_c c on a.idfielda=c.idfieldc
left join tbl_d d on a.userEntry=d.NoIdUser

錯誤信息:

W (1): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'when 'O' then

查詢中有一些語法錯誤。 a.seqreq之后缺少逗號(,)。 在第二個case語句中,當 'O' 然后 a.online_date 時將是case upper(a.via) when 'O' then a.online_date而不是case when upper(a.via) when 'O' then a.online_date

SELECT a.seqreq,
b.idfieldb ,
c.idfieldc ,
a.number_given,
case upper(a.via) 
when 'O' then 'Online'
when 'M' then 'Manual'
else a.via
end as via,
case  upper(a.via) when 'O' then a.online_date
when 'M' then a.manual_date 
else a.others_date
end as date_registered
from mytablea a 
left join tbl_b b on a.idfielda=b.idfieldb
left join tbl_c c on a.idfielda=c.idfieldc
left join tbl_d d on a.userEntry=d.NoIdUser

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM