gpt4 book ai didi

mysql - LPAD/RPAD : Align column

转载 作者:行者123 更新时间:2023-11-29 04:42:55 26 4
gpt4 key购买 nike

我想对齐我的专栏“Gesamt”:

SELECT k.KUNDENID AS ID, 
vorname AS Vorname,
nachname AS Nachname,
CONCAT(IFNULL(ROUND(SUM(Betrag), 2), 0), LPAD('Euro', 4, ' ')) AS Gesamt
FROM kunde k
LEFT JOIN verkauf v ON k.KUNDENID = v.KUNDENID
GROUP BY k.KUNDENID;

输出:

+----+---------+-----------+-------------+
| ID | Vorname | Nachname | Gesamt |
+----+---------+-----------+-------------+
| 1 | Homer | Simpson | 20.95Euro |
| 2 | Doug | Heffernan | 120.50Euro |
| 3 | Dexter | Morgan | 350.30Euro |
| 4 | Sheldon | Cooper | 1500.00Euro |
| 5 | Maurice | Moss | 0.00Euro |
+----+---------+-----------+-------------+

所以我想要的输出看起来像:

+----+---------+-----------+--------------+
| ID | Vorname | Nachname | Gesamt |
+----+---------+-----------+--------------+
| 1 | Homer | Simpson | 20.95 Euro |
| 2 | Doug | Heffernan | 120.50 Euro |
| 3 | Dexter | Morgan | 350.30 Euro |
| 4 | Sheldon | Cooper | 1500.00 Euro |
| 5 | Maurice | Moss | 0.00 Euro |
+----+---------+-----------+--------------+

如何正确对齐?

感谢您的帮助!

最佳答案

通过连接 '<space>Euro' 来简化连接而不是 LPAD('Euro',4,' ')并在整个 concat 中添加一个 LPAD 使其右移(您可以根据您的数据决定需要多少个字符才能对齐):

SELECT k.KUNDENID AS ID, 
vorname AS Vorname,
nachname AS Nachname,
LPAD(
CONCAT(IFNULL(ROUND(SUM(Betrag), 2), 0), ' Euro')
, 13, ' ') AS Gesamt
FROM kunde k
LEFT JOIN verkauf v ON k.KUNDENID = v.KUNDENID
GROUP BY k.KUNDENID;

关于mysql - LPAD/RPAD : Align column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24694305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com