简体   繁体   中英

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

$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)

$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

<?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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