Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在SQL中将数值结果与字符串连接_Mysql_Sql_String_Sum_Subquery - Fatal编程技术网

Mysql 如何在SQL中将数值结果与字符串连接

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

我想在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(
        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:当你尝试这个解决方案时发生了什么?