Home > Database > Mysql Tutorial > body text

MySQL numeric operators and functions

黄舟
Release: 2017-02-27 13:22:08
Original
1483 people have browsed it

For ordinary addition and subtraction operations, we will not explain them in detail here, as shown in the following example:

mysql> SELECT 3+4;
+-----+
| 3+4 |
+-----+
|   7 |
+-----+
1 row in set (0.03 sec)
Copy after login


CEIL and FLOOR

CEIL is rounded up, as long as there are decimal places , no matter how many, enter one directly. For example:

mysql> SELECT CEIL(3.01);
+------------+
| CEIL(3.01) |
+------------+
|          4 |
+------------+
1 row in set (0.02 sec)
Copy after login


FLOOR is the opposite of


mysql> SELECT FLOOR(3.99);
+-------------+
| FLOOR(3.99) |
+-------------+
|           3 |
+-------------+
1 row in set (0.00 sec)
Copy after login


p and MOD

p is rounded, MOD is remainder

mysql> SELECT 3/4;
+--------+
| 3/4    |
+--------+
| 0.7500 |
+--------+
1 row in set (0.00 sec)

mysql> SELECT 3 p 4;
+---------+
| 3 p 4 |
+---------+
|       0 |
+---------+
1 row in set (0.00 sec)

mysql> SELECT 3 MOD 4;
+---------+
| 3 MOD 4 |
+---------+
|       3 |
+---------+
1 row in set (0.00 sec)
Copy after login

Note: MOD can be replaced by %

POWER exponentiation

For example, 3 to the 2nd power

mysql> SELECT POWER(3,2);
+------------+
| POWER(3,2) |
+------------+
|          9 |
+------------+
1 row in set (0.14 sec)
Copy after login

ROUNDRounding

For example, 3.652 is rounded to two decimal places.

mysql> SELECT ROUND(3.652,2);
+----------------+
| ROUND(3.652,2) |
+----------------+
|           3.65 |
+----------------+
1 row in set (0.00 sec)
Copy after login

Retain one decimal place:

mysql> SELECT ROUND(3.652,1);
+----------------+
| ROUND(3.652,1) |
+----------------+
|            3.7 |
+----------------+
1 row in set (0.00 sec)
Copy after login


##TRUNCATE

Remove certain digits directly

mysql> SELECT TRUNCATE(125.89,2);
+--------------------+
| TRUNCATE(125.89,2) |
+--------------------+
|             125.89 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT TRUNCATE(125.89,1);
+--------------------+
| TRUNCATE(125.89,1) |
+--------------------+
|              125.8 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT TRUNCATE(125.89,0);
+--------------------+
| TRUNCATE(125.89,0) |
+--------------------+
|                125 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT TRUNCATE(125.89,-1);
+---------------------+
| TRUNCATE(125.89,-1) |
+---------------------+
|                 120 |
+---------------------+
1 row in set (0.00 sec)
Copy after login
The above is the content of MySQL numerical operators and functions. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!




source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!