Pregunta

this is my query

$sum = "SELECT htno, 
SUM(tm) AS tech, 
SUM(tm)/7.5 AS divi, 
SUM(credits) AS cred , 
SUM(CASE WHEN credits <= 0 THEN 1 ELSE 0 END) AS log,
SUM(CASE WHEN credits > 0 THEN 1 ELSE 0 END) AS pass, 
SUM(CASE WHEN em >= 0 THEN 1 ELSE 0 END) AS atm, 
SUM(CASE WHEN em >= -2 THEN 1 ELSE 0 END) AS tot
FROM jbit WHERE htno='$id'";

in 3rd line 'SUM(tm)/7.5 As divi' query is giving output as 79.2544555555 but i want output as 79.25

Help me friends

¿Fue útil?

Solución

You could use TRUNCATE() :

TRUNCATE(SUM(tm)/7.5 , 2) AS divi,

Truncate will give you '1.25' for '1.259' , if you want it round it then use:

ROUND(SUM(tm)/7.5 , 2) AS divi,

that will give you '1.26' for '1.259'

Otros consejos

You need MySQL's FORMAT():

Formats the number X to a format like '#,###,###.##', rounded to D decimal places, and returns the result as a string.

So your query becomes:

$sum = "SELECT htno, 
SUM(tm) AS tech, 
FORMAT(SUM(tm)/7.5, 2) AS divi, 
SUM(credits) AS cred , 
SUM(CASE WHEN credits <= 0 THEN 1 ELSE 0 END) AS log,
SUM(CASE WHEN credits > 0 THEN 1 ELSE 0 END) AS pass, 
SUM(CASE WHEN em >= 0 THEN 1 ELSE 0 END) AS atm, 
SUM(CASE WHEN em >= -2 THEN 1 ELSE 0 END) AS tot
FROM jbit WHERE htno='$id'";
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top