MySQL - 千位分隔符

发布于 2024-11-18 16:31:05 字数 680 浏览 2 评论 0原文

我想知道是否有办法在 SQL 查询中获取千位分隔符?

请求如下所示:

select coalesce(user, 'TOTAL') as "user",
    concat(count(items), ' items') as "nb_items",
    concat(replace(sum(amount), '.', ','), ' €') as "total_in_euro",
    concat(
        round((sum(amount) / (select sum(amount) from my_db) * 100), 2),
        ' %'
    ) as "share"
from my_db
group by user
with rollup

目前,CSV 结果是:

user;nb_items;total_in_euro
usr_1;20 items;20000 €;10,01 %
usr_2;254 items;17852,12 €;8,12 %

我想要的是这样的结果:

user;nb_items;total_in_euro
usr_1;20 items;20 000 €;10,01 %
usr_2;254 items;17 852,12 €;8,12 %

有办法做到这一点吗?

I would like to know if there is a way to get a thousand separator in an SQL Query ?

The request looks like this :

select coalesce(user, 'TOTAL') as "user",
    concat(count(items), ' items') as "nb_items",
    concat(replace(sum(amount), '.', ','), ' €') as "total_in_euro",
    concat(
        round((sum(amount) / (select sum(amount) from my_db) * 100), 2),
        ' %'
    ) as "share"
from my_db
group by user
with rollup

Currently, the CSV result is:

user;nb_items;total_in_euro
usr_1;20 items;20000 €;10,01 %
usr_2;254 items;17852,12 €;8,12 %

What I want is a result like that :

user;nb_items;total_in_euro
usr_1;20 items;20 000 €;10,01 %
usr_2;254 items;17 852,12 €;8,12 %

Is there a way to do so ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔姬 2024-11-25 16:31:05

我不知道如何用空格来实现,但标准(用点或逗号 [德国即])分隔可以通过 FORMAT() 实现。

查看格式化函数

mysql> SELECT FORMAT(12332.123456, 4);
    -> '12,332.1235'
mysql> SELECT FORMAT(12332.1,4);
    -> '12,332.1000'
mysql> SELECT FORMAT(12332.2,0);
    -> '12,332'
mysql> SELECT FORMAT(12332.2,2,'de_DE');
    -> '12.332,20'
mysql> SELECT FORMAT(12332.2,2,'pt_BR');
    -> '12332,20'

I don't know how to do it with a space, but the standard (with a point or a comma [germany i.e.]) seperation can be achieved with FORMAT().

Have a look at the Format Function

mysql> SELECT FORMAT(12332.123456, 4);
    -> '12,332.1235'
mysql> SELECT FORMAT(12332.1,4);
    -> '12,332.1000'
mysql> SELECT FORMAT(12332.2,0);
    -> '12,332'
mysql> SELECT FORMAT(12332.2,2,'de_DE');
    -> '12.332,20'
mysql> SELECT FORMAT(12332.2,2,'pt_BR');
    -> '12332,20'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文