Performing simple arithmetic in MySQL statement or in PHP code

后端 未结 5 1568
野性不改
野性不改 2020-12-21 07:47

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

5条回答
  •  别那么骄傲
    2020-12-21 08:33

    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.

提交回复
热议问题