【问题标题】:How to round mysql column in a table output如何在表输出中舍入 mysql 列
【发布时间】:2012-04-22 03:31:48
【问题描述】:

我需要在表数组输出中舍入 SQL 舍入的结果。我无法弄清楚语法...

$result = mysql_query("SELECT `Energ_Kcal`*`yield`*`qty` AS `cal` FROM allinnot a
WHERE `own_id` = $user->id");

    echo "<tr><td>" . $row['Shrt_Desc'] . "</td><td>&nbsp;" . $row['desc'] . "</td><td>" . $row['cal'] . " cal</td></tr>";

cal 返回一个包含许多小数点后数字的值。我只需要它来显示一个整数。我试过 ROUND(),但一定是放错地方了。

【问题讨论】:

    标签: php mysql html-table


    【解决方案1】:

    ROUND 应该围绕这些值,试试这个:

    $result = mysql_query("SELECT ROUND(`Energ_Kcal`*`yield`*`qty`,2) AS `cal` FROM allinnot a
    WHERE `own_id` = $user->id");
    
        echo "<tr><td>" . $row['Shrt_Desc'] . "</td><td>&nbsp;" . $row['desc'] . "</td><td>" . $row['cal'] . " cal</td></tr>";
    

    【讨论】:

      【解决方案2】:

      您还可以查看 PHP 的 round() 或者,可能来自您的描述 int_val()

      【讨论】:

        【解决方案3】:

        一般语法

        ROUND( expression, 2 )
        

        【讨论】:

        • 哎呀,谢谢!我也试图在内部使用 AS cal。 ROUND(Energ_Kcal*yield*qty) AS cal 完美运行。
        猜你喜欢
        • 2021-03-31
        • 2010-12-27
        • 1970-01-01
        • 1970-01-01
        • 2015-12-11
        • 2020-11-24
        • 2023-01-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多