Truncated incorrect DOUBLE value: ‘xx’
问题
表中存储分数,普通分数用90、80、60表示,特别优秀的分数时S和SS表示,现在需要分级显示,模拟的查询语句如下
select name, score,
case score
when 90 then 'A'
when 80 then 'B'
when 60 then 'C'
when 'S' then 'S+'
when 'SS' then 'SS+'
else 'E'
end as '分数'
from score ;
用Navicat查询时没有问题,直接输出,但是结果中包含S或SS的数据显示不是S+或SS+,发现问题后再用DBeaver查询,结果也可以输出,虽然也不正确,但多了一条提示
Truncated incorrect DOUBLE value: 'S'
分析
搜索了一下报错,这个报错多半是语法格式问题,例如使用了多余的AND连接,例如
错误:
update user_detail set name = ? and phone_code = ? and phone_num = ? and email = ? where emp_code = ?
正确:
update user_detail set name = ?, phone_code = ?, phone_number = ?, email = ? where emp_code = ?
但是这里没有类似的情况,提示和S有关,只能是在case when的条件中出错了,同时使用了整型和字符的原因,查看score的类型是varchar,需要修改查询语句中的case when的条件类型
解决
语句中使用的数据类型错误,将case when中的条件统一为字符类型,如下
错误:
select name, score,
case score
when 90 then 'A'
when 80 then 'B'
when 60 then 'C'
when 'S' then 'S+'
when 'SS' then 'SS+'
else 'E'
end as '分数'
from score ;
正确:
select name, score,
case score
when '90' then 'A'
when '80' then 'B'
when '60' then 'C'
when 'S' then 'S+'
when 'SS' then 'SS+'
else 'E'
end as '分数'
from score ;