gpt4 book ai didi

php - 在 MySQL 或 PHP 中隐藏 CAST SELECT 输出的前导 0

转载 作者:行者123 更新时间:2023-11-29 02:56:44 25 4
gpt4 key购买 nike

早上好

我目前有一个 SELECT 语句计算运动表的 PCT:

SELECT team
, COUNT(*) as played
, SUM(win) as wins
, SUM(loss) as lost
, SUM(draw) as draws
, SUM(SelfScore) as ptsfor
, SUM(OpponentScore) as ptsagainst
, SUM((win*2 + draw)- loss) as score
, CAST(SUM(win + (draw/2))/SUM(win + loss + draw) as decimal(4,3)) as pctWon

如果球队赢得了所有比赛,则显示为 1.000,这是正确的。

如果结果不是 1.000,我想删除前导 0。因此,对于 50% 的获胜,它将显示为 .500,而不是当前显示的 0.500。

我不确定用 PHP 还是 MySQL 最容易。

谢谢

最佳答案

这应该有效:

SELECT team
, COUNT(*) as played
, REPLACE(SUM(win), '0.', '.') as wins
, SUM(loss) as lost
, SUM(draw) as draws
, SUM(SelfScore) as ptsfor
, SUM(OpponentScore) as ptsagainst
, SUM((win*2 + draw)- loss) as score
, CAST(SUM(win + (draw/2))/SUM(win + loss + draw) as decimal(4,3)) as pctWon

关于php - 在 MySQL 或 PHP 中隐藏 CAST SELECT 输出的前导 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30001960/

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