将带有千位分隔符的值插入表

时间:2018-08-16 08:26:33

标签: mysql sql

我使用以下SQL创建具有值的表:

CREATE TABLE Logistics (
    country TEXT,
    Costs_Inbound CHAR(255),
    Costs_Storage CHAR(255),
    Costs_Outbound DECIMAL(65,3)

);


INSERT INTO Logistics 
(country, Costs_Inbound, Costs_Storage, Costs_Outbound)
VALUES 
("DE", "5000", "300", "600500.815"),
("NL", "3000", "650", "250452.454"), 
("FR", "4000", "120", "750060.932");

和以下SQL从表中查询值:

SELECT country, sum(Costs_Inbound), sum(Costs_Storage), sum(Costs_Outbound),
       (sum(Costs_Inbound) + sum(Costs_Storage) + sum(Costs_Outbound)) as Total
FROM Logistics
GROUP BY country WITH ROLLUP;

您还可以在sqlfiddle here中找到代码。
到目前为止,所有这些工作正常。


现在,我希望这些值使用千位分隔符进入表格,因此它们应显示为{而不是6000500,815250452.454750060.932 {1}},600,500.815250,452.454

您知道我如何实现这一目标吗?

1 个答案:

答案 0 :(得分:0)

使用format函数

SELECT country, FORMAT (sum(Costs_Inbound),2) Costs_Inbound, 
FORMAT(sum(Costs_Storage),2) Costs_Storage,
FORMAT(sum(Costs_Outbound), 2) Costs_Outbound,
      FORMAT( (sum(Costs_Inbound) + sum(Costs_Storage) + sum(Costs_Outbound)),2) as Total
FROM Logistics
GROUP BY country WITH ROLLUP

http://sqlfiddle.com/#!9/92d35b2/7