Skip to content
Advertisement

MYSQL case when error when tried to get value based on condition

Why I am getting error with this query ? Is there something I’m missing ?

So I wanted to get online_date if the “via” field is O, then if the “via” field is M I wanted to get the 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

the error message :

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

Advertisement

Answer

There are some syntax error in query. An comma(,) is missing after a.seqreq. In second case statement it will be case upper(a.via) when 'O' then a.online_date instead of 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
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement