简体   繁体   English

在 MySQL 语句或 PHP 代码中执行简单的算术运算

[英]Performing simple arithmetic in MySQL statement or in PHP code

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.例如,假设我有一个包含两个 integer 列startend的数据结构,但我实际上对 start 和 start 和 end 值之间的差异感兴趣。

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.并在 PHP 中执行$end - $start操作。 Is it better to leave these sorts of simple operations to MySQL?将这些简单的操作留给 MySQL 会更好吗?

I would classify "$end - $start" as business logic, and that belongs in the model layer not the persistence layer.我会将“$end - $start”归类为业务逻辑,它属于 model 层而不是持久层。 That means performing the calculation in PHP.这意味着在 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.您可以更彻底地进行单元测试。

I'm one of those database should do data things so unless you're using an aggregate function most things like this should be done on the application end.我是其中一个数据库应该做数据的事情,所以除非你使用聚合 function 大多数这样的事情应该在应用程序端完成。 Now for simplicity here your query could be just现在为简单起见,您的查询可能只是

SELECT start, (end - start) AS difference FROM foo;

but for more complicated things it's recommended that you do a lot of the data manipulation in the application side.但对于更复杂的事情,建议您在应用程序端进行大量数据操作。

The other answer says to put the logic as close to the DB as possible (which IMO is a horrid idea) although in this case I see nothing wrong with what you're doing but just remember that the database is already doing a lot of work to just get you the data, if you're going to be doing a lot of logic on the database it's going to make it just that much slower.另一个答案说将逻辑尽可能靠近数据库(IMO 是一个可怕的想法)虽然在这种情况下我认为你正在做的事情没有任何问题,但请记住数据库已经做了很多工作只是让你得到数据,如果你要在数据库上做很多逻辑,它会让它变得慢得多。 For small applications it's not a big deal but for large scale applications you will see implications very quickly not only in performance but adding new functionality (splitting logic between database and application level gets very confusing and hard to refactor).对于小型应用程序来说,这没什么大不了的,但对于大型应用程序,您会很快看到影响,不仅在性能方面,而且在添加新功能方面(数据库和应用程序级别之间的拆分逻辑变得非常混乱且难以重构)。

Normally any view transformation should be handled in presentation layer.通常,任何视图转换都应在表示层中处理。 In your case your model is designed with start & end fields, so you should expose this fields from sql.在您的情况下,您的 model 设计有开始和结束字段,因此您应该从 sql 公开这些字段。

MySQL can perform some basic arithmetic. MySQL 可以执行一些基本的算术运算。 However, it is definitely better to perform that sort of arithmetic in PHP if you're already using the data in PHP anyway.但是,如果您已经在使用 PHP 中的数据,那么在 PHP 中执行这种算术肯定会更好。

MySQL is meant more for simply storing and extracting relational data. MySQL 更多的是用于简单地存储和提取关系数据。

For simple operations I would use MYSQL.对于简单的操作,我会使用 MYSQL。 I'm not a guru, but I like to keep things as simple and clean as posible so where it is possible I use MYSQL built-in functions to do the stuff that can be done with php.我不是专家,但我喜欢尽可能保持简单和干净,所以我尽可能使用 MYSQL 内置函数来完成可以使用 php 完成的工作。 In other words - using MYSQL would lower the amount of code and formatting necessary to display the data.换句话说 - 使用 MYSQL 将减少显示数据所需的代码量和格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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