For instance, say I have a data structure with two integer columns start and end, but I’m actually interested in the start and difference between start and end values.
I could send this query:
SELECT start, end - start FROM foo;
or, I could just do
SELECT start, end FROM foo;
and perform the $end - $start operation in PHP. Is it better to leave these sorts of simple operations to MySQL?
I would classify “$end – $start” as business logic, and that belongs in the model layer not the persistence layer. That means performing the calculation in PHP. This has a number of benefits:
If you change databases later, you don’t need the same operators to exist.
You can source control the logic that performs the calculation.
You can more thoroughly unit test.