【问题标题】:MYSQL case when error when tried to get value based on condition尝试根据条件获取值时出错的MYSQL案例
【发布时间】:2021-04-25 15:52:12
【问题描述】:

为什么我收到此查询错误?有什么我想念的吗?

所以如果“via”字段为 O,我想获取 online_date,然后如果“via”字段为 M,我想获取 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

【问题讨论】:

  • case when upper(a.via) when 'O' then a.online_date => case upper(a.via) when
  • 感谢塞尔格!我是多么愚蠢,没想到会这样:3

标签: mysql sql case


【解决方案1】:

查询中有一些语法错误。 a.seqreq 后缺少逗号 (,)。在第二个 case 语句中,它将是 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

【讨论】:

  • 谢谢 Kazi 没看到那个:3 天哪,尴尬
猜你喜欢
  • 2018-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-06
  • 1970-01-01
  • 1970-01-01
  • 2016-05-26
  • 1970-01-01
相关资源
最近更新 更多