简体   繁体   English

在PHP SQL中使用ROUND()

[英]using ROUND() in php sql

Tried juggling the order around but it doesn't seem to work. 试图弄乱订单,但似乎不起作用。 Attempting to trunc the result to a full number rather than a decimal. 尝试将结果截断为整数而不是小数。

Current query in php that works to give me a percentage PHP中的当前查询可以给我一定的百分比

$query_total = "SELECT (COUNT(".$id.")* 100 / (SELECT COUNT(*) FROM ".$att_tb.")) AS attOverall FROM ".$att_tb;

My attempt at using ROUND() to trunc the result (not working) 我尝试使用ROUND()截断结果(不起作用)

$query_total = "SELECT CAST(ROUND(COUNT(".$id.")* 100 / (SELECT COUNT(*) FROM "$att_tb."),0) AS attOverall) FROM ".$att_tb;

A working correction would be greatly appreciated, thank you. 进行中的纠正工作将不胜感激,谢谢。

USE FLOOR 使用地板

http://php.net/manual/en/function.floor.php http://php.net/manual/zh/function.floor.php

<?php
echo floor(4.3);   // 4
echo floor(9.999); // 9
echo floor(-3.14); // -4
?>

I would write this as: 我可以这样写:

SELECT AVG(CASE WHEN $id IS NOT NULL THEN 100.0 ELSE 0 END) AS attOverall
FROM ".$att_tb;

You can then cast the value to whatever type you like or use FORMAT() to format it as a string. 然后,您可以将值转换为所需的任何类型,或使用FORMAT()将其格式化为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM