MySQL中使用concat拼接字符集
用mysql统计数据总和,需要拼接中文,发现是乱码,一直以为自己写的sql有问题,最后一查原来是因为使用concat函数时,需要转换参数类型。
1.乱码sql
直接使用concat拼接中文和统计结果:
select
concat('全部',sum(count_num)) as plan_type
from
(select
case when plan_type ='编制' then '编制'
when plan_type ='审核' then '审核'
when plan_type ='执行' then '执行'
when plan_type ='作废' then '作废'
end plan_type,
count_num
from
(select
plan_type,count(1)
from
t_td_plan
group by plan_type)t)h
1.1运行结果
运行出来的结果集是乱码
2.修改后的sql
使用concat函数时,需要转换参数类型:
select
concat('全部',cast(sum(count_num) as char)) as plan_type
from
(select
case when plan_type ='编制' then '编制'
when plan_type ='审核' then '审核'
when plan_type ='执行' then '执行'
when plan_type ='作废' then '作废'
end plan_type,
count_num
from
(select
plan_type,count(1)
from
t_td_plan
group by plan_type)t)h
2.1运行结果
运行出来的结果集正常