Mysql 如何在SQL中将数值结果与字符串连接
我想在MySQL中将float的结果与string对象连接起来 接口 我写了这个查询:Mysql 如何在SQL中将数值结果与字符串连接,mysql,sql,string,sum,subquery,Mysql,Sql,String,Sum,Subquery,我想在MySQL中将float的结果与string对象连接起来 接口 我写了这个查询: select name, concat(str(round(sum(amount_paid)/(select sum(amount_paid) from order_items)*100.0,2)) ,'%') as pct from order_items group by 1 order by 2 desc; 谁能给我一个可靠的查询,因为我似乎遗漏了什么。str()在MySQL中不是一个东西concat
select name, concat(str(round(sum(amount_paid)/(select sum(amount_paid) from order_items)*100.0,2)) ,'%') as pct
from order_items
group by 1
order by 2 desc;
谁能给我一个可靠的查询,因为我似乎遗漏了什么。str()
在MySQL中不是一个东西concat()
强制转换为字符串,因此您只需执行以下操作:
select name,
concat(
round(
sum(amount_paid) / (select sum(amount_paid) from order_items) * 100,
2
),
'%'
) as pct
from order_items
group by name
order by pct desc;
请注意,如果您运行的是MySQL 8.0,则可以使用窗口总和而不是子查询:
select name,
concat(
round(
sum(amount_paid) / sum(sum(amount_paid)) over() * 100,
2
),
'%'
) as pct
from order_items
group by name
order by pct desc;
谢谢@GMB,我尝试了没有str的concat,但这一定是因为我的虚拟环境有一些限制。@RakeshDash:当你尝试这个解决方案时发生了什么?