Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

peterpan26's avatar

query bug?? rounded to the closest integer?

in the following case: in query: 20+80+90 (we all know gives 190) / divided by 25 (we all can calculate and gives 7.6) in the case of setting every column on phpmyadmin to decimal 5,2 and trying to perform a query in wich case can this happen? (give a result of 7.0) instead of 7.6, tried both eloquent | normal query| and created view, all the querys gave 7.0 instead of 7.6 , what am i doing wrong??? i tried also making query with convert option to decimal too

0 likes
8 replies
simonagjerner's avatar

Like its counterpart floor, ceiling is a mathematical operation that takes a number and rounds it up to the nearest integer. For example, the ceiling of 5 is 5, and so is the ceiling of 4.1.

SQL uses the CEILING function to perform this computation on Myaccountaccess. It takes a single argument: the column whose values you’d like to round up to the nearest integer.

In our example, we’d like to calculate how many square meters (rounded up to the nearest integer) there are per one bike. In our example, we used CEILING like so: CEILING(area/bikes_for_rent)). This returns an integer result, not a float.

peterpan26's avatar
                'costKM' => Repairs::query()
                    ->join('form', 'form.carplate', '=', 'form.carplate')
                    ->selectRaw("(convert(form.totalFuel, decimal(5,2))+convert(form.ports, decimal(5,2))+convert(repairs.cost, decimal(5,2)))/convert(form.endKM-form.currentKM, decimal(5,2)) AS costKM")
                    ->orderBy('form.carplate')
                    ->groupBy('form.carplate')

                    ->get(),
            ]);
Tray2's avatar

@peterpan26 Are you using a SQLite database?

I made two tables

t1 and t2, t1 contained three columns with the values 20, 80 and 90, and t2 containing the value of 25.

Then I ran this query on it.

SELECT (t1.val1 + t1.val2 + t1.val3) / t2.val1 as total
FROM t1
JOIN t2 on t1.id = t2.id;

With SQLite I got the result 7, and with MySQL/MariaDB I got 7.6000.

peterpan26's avatar

@Tray2 that's intresting i will try to see that later and install mariadb although it says its mariadb. thanks a lot

Tray2's avatar

@peterpan26 If you are using SQLite you need to store the values as real, or it will only give you ints.

lbecket's avatar

@peterpan26 I don't think that you need to use convert. You should be able to reference the values directly. So, something like:

$costKM = Repairs::query()
$costKM->join('form', 'form.carplate', '=', 'form.carplate')
    ->selectRaw("(form.totalFuel + form.ports + repairs.cost)/(form.endKM - form.currentKM) AS costKM")
    ->orderBy('form.carplate')
    ->groupBy('form.carplate')
	->get();

Please or to participate in this conversation.