MySQL Handy math function

Source: Internet
Author: User
Tags square root

Recently the project is very busy, the function that gives me is more complex, fortunately can cope with down. In the process of working, I found that the use of MySQL's own function can greatly reduce the complexity of the program. This is inevitable, using the MySQL function, can save a lot of loop traversal in the program. But some experts on the internet say that using math and string functions in MySQL will slow down MySQL's efficiency, which I think is inevitable. To the original program should be done to the work of MySQL to do, MySQL dry more, then the lower the efficiency.

So is there any possibility of finding a balance? Not recommended, because the big god says that MySQL's function is inefficient and not used. If this is the case, then simply do not even MySQL database use it, than MySQL mature database catch a lot. But why use MySQL? Free (not for free now), both use MySQL's advantages and try to avoid MySQL's inefficient pits. That is, in the use of the system, to ask clearly the scope of the application of the software.

In the case of internal systems, it is common to know that internal systems often have complex business logic and are very granular, but with a small amount of traffic, that is, a smaller amount of concurrency. The external system may not logically be as complex as the internal system, but the traffic is much larger and more concurrent. If it is a small amount of access to the software, you can use the system as much as possible with the MySQL self-function, the full reduction of the complexity of the program, but also conducive to maintenance. If it is a large amount of access to the program should be as far as possible to calculate the logical logic of the resources in the program to run, as far as possible to the service-side non-confidential computing on the user side to run. Let the client be responsible for the calculation, let the program segment responsible for logic, so that the DB side only responsible for data storage. Each has its own roles and uses the director.

In short, can not unworthy, not because someone else's sentence, just conform to the lazy heart of their own do not want to learn.

MySQL math function walk, first write a part, later slowly Add.

1, the format (x, y) function, the function is to place a number x, the y decimal place is reserved, and the integer part is separated by commas, the fractional part is rounded.

It is important to note that once your data is separated by thousands of bits, it becomes a string. can provide a better experience for reading, but it is very difficult to calculate, so if you just keep the decimal, it is not recommended to use this function.

2, ABS (); To find the absolute value of a number; absolute

3, sqrt (); Ask for the square root of a number. SQRT is the abbreviation for Sqruar (square, rectangle), root (Root).

4, mod (x, y) × divisor, y dividend. The end is the remainder.

5, Ceil () into a rounding.

Floor () Rounding and rounding

These two functions are mirror functions, which are somewhat more interesting. These two functions are not rounded and are more robust.

6, Rand () as the name implies, is used to generate random numbers.

7, format will automatically do thousands of bits, the following we look at the round function, to be rounded.

8, truncate (x, y) compare overbearing, regardless of rounding, directly to X, the y decimal place directly killed.

9, sign () returns the current result of the symbol, if a negative number returns-1, if 0 returns 0 if it is positive, returns 1.

10. Power () Power operation

MySQL Handy math function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.