mysql的內置函數


字符串函數

Concat()  字符串連接 

mysql> select * from name;
+------+-------+-------+
| id   | name  | major |
+------+-------+-------+
|    1 | Jack  | CS    |
|    2 | Paul  | MS    |
|    3 | Linda | SE    |
|    7 | Robin | CS    |
+------+-------+-------+
4 rows in set (0.00 sec)

mysql> select concat(id,name) from name;
+-----------------+
| concat(id,name) |
+-----------------+
| 1Jack           |
| 2Paul           |
| 3Linda          |
| 7Robin          |
+-----------------+
4 rows in set (0.05 sec)

mysql>

 

Lcase()    轉小寫

mysql> select * from name;
+------+-------+-------+
| id   | name  | major |
+------+-------+-------+
|    1 | Jack  | CS    |
|    2 | Paul  | MS    |
|    3 | Linda | SE    |
|    7 | Robin | CS    |
+------+-------+-------+
4 rows in set (0.00 sec)

mysql> select lcase(name) from name where id =1;
+-------------+
| lcase(name) |
+-------------+
| jack        |
+-------------+
1 row in set (0.01 sec)

mysql>

 

Ucase()   轉大寫

mysql> select * from name;
+------+-------+-------+
| id   | name  | major |
+------+-------+-------+
|    1 | Jack  | CS    |
|    2 | Paul  | MS    |
|    3 | Linda | SE    |
|    7 | Robin | CS    |
+------+-------+-------+
4 rows in set (0.00 sec)

mysql> select ucase(name) from name where id =1;
+-------------+
| ucase(name) |
+-------------+
| JACK        |
+-------------+
1 row in set (0.00 sec)

mysql>

 

Length()   字符串長度

mysql> select * from name;
+------+-------+-------+
| id   | name  | major |
+------+-------+-------+
|    1 | Jack  | CS    |
|    2 | Paul  | MS    |
|    3 | Linda | SE    |
|    7 | Robin | CS    |
+------+-------+-------+
4 rows in set (0.00 sec)

mysql> select length(name) from name;
+--------------+
| length(name) |
+--------------+
|            4 |
|            4 |
|            5 |
|            5 |
+--------------+
4 rows in set (0.02 sec)

 

Ltrim()    去除左邊空格

mysql> select * from name;
+------+-------+-------+
| id   | name  | major |
+------+-------+-------+
|    1 | Jack  | CS    |
|    2 | Paul  | MS    |
|    3 | Linda | SE    |
|    7 | Robin | CS    |
+------+-------+-------+
4 rows in set (0.00 sec)
mysql> select ltrim(name) from name;
+-------------+
| ltrim(name) |
+-------------+
| Jack        |
| Paul        |
| Linda       |
| Robin       |
+-------------+
4 rows in set (0.02 sec)

 

Rtrim()   去除右邊空格

mysql> select * from name;
+------+-------+-------+
| id   | name  | major |
+------+-------+-------+
|    1 | Jack  | CS    |
|    2 | Paul  | MS    |
|    3 | Linda | SE    |
|    7 | Robin | CS    |
+------+-------+-------+
4 rows in set (0.00 sec)

mysql> select rtrim(name) from name;
+-------------+
| rtrim(name) |
+-------------+
| Jack        |
| Paul        |
| Linda       |
| Robin       |
+-------------+
4 rows in set (0.00 sec)

 

Repeat(string,count)   重復count次  (沒有貼原表 同上)

mysql> select repeat(name,2) from name;
+----------------+
| repeat(name,2) |
+----------------+
| JackJack       |
| PaulPaul       |
| LindaLinda     |
| RobinRobin     |
+----------------+
4 rows in set (0.00 sec)

mysql>

 

Replace(str,search_str,replace_str)   在str中用replace_str替換searche_str        區分大小寫

mysql> select replace(name,"Jack","Tom") from name;
+----------------------------+
| replace(name,"Jack","Tom") |
+----------------------------+
| Tom                        |
| Paul                       |
| Linda                      |
| Robin                      |
+----------------------------+
4 rows in set (0.00 sec)

 

Substring(str,position,length)  從position開始,截取length個字符        (第一位從1開始)

mysql> select substring(name,1,3) from name;
+---------------------+
| substring(name,1,3) |
+---------------------+
| Jac                 |
| Pau                 |
| Lin                 |
| Rob                 |
+---------------------+
4 rows in set (0.00 sec)

mysql>

 

Space(count)     生成count個空格

 

mysql> select space(2) ;
+----------+
| space(2) |
+----------+
|          |
+----------+
1 row in set (0.00 sec)

mysql> select space(20) ;
+----------------------+
| space(20)            |
+----------------------+
|                      |
+----------------------+
1 row in set (0.00 sec)

 

數學函數

BIN(decimal_number)   十進制轉二進制

mysql> select bin(255);
+----------+
| bin(255) |
+----------+
| 11111111 |
+----------+
1 row in set (0.00 sec)

 

CEILING(number)          向上取整       ceiling(天花板)

mysql> select CEILING(2.34);
+---------------+
| CEILING(2.34) |
+---------------+
|             3 |
+---------------+
1 row in set (0.01 sec)

 

FLOOR(number)           向下取整

mysql> select FLOOR(2.34);
+-------------+
| FLOOR(2.34) |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)

 

MAX(num1,num2)         取最大值

mysql> select max(id) from name;
+---------+
| max(id) |
+---------+
|       7 |
+---------+
1 row in set (0.00 sec)

 

MIN(num1,num2)          取最小值

mysql> select min(id) from name;
+---------+
| min(id) |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

 

SQRT(number)             開平方

mysql> select sqrt(2);
+--------------------+
| sqrt(2)            |
+--------------------+
| 1.4142135623730951 |
+--------------------+
1 row in set (0.02 sec)

 

RAND()                    返回0-1內的隨機數

 

mysql> select rand();
+-------------------+
| rand()            |
+-------------------+
| 0.979456949028612 |
+-------------------+
1 row in set (0.00 sec)

 

日期函數

Curdate()   返回當前日期

mysql> select Curdate();
+------------+
| Curdate()  |
+------------+
| 2018-09-01 |
+------------+
1 row in set (0.01 sec)

 

Curtime()   返回當前時間

mysql> select Curtime();
+-----------+
| Curtime() |
+-----------+
| 11:48:13  |
+-----------+
1 row in set (0.00 sec)

 

Now()      返回當前日期時間

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2018-09-01 11:48:37 |
+---------------------+
1 row in set (0.00 sec)

 

Unix_timestamp(date)  返回當前date的時間戳

mysql> select Unix_timestamp();
+------------------+
| Unix_timestamp() |
+------------------+
|       1535773773 |
+------------------+
1 row in set (0.00 sec)
mysql> select Unix_timestamp("2018-03-04 03:40:20");
+---------------------------------------+
| Unix_timestamp("2018-03-04 03:40:20") |
+---------------------------------------+
|                            1520106020 |
+---------------------------------------+
1 row in set (0.00 sec)

 

From_unixtime()       返回時間戳的日期值

mysql> select From_unixtime(1520106020);
+---------------------------+
| From_unixtime(1520106020) |
+---------------------------+
| 2018-03-04 03:40:20       |
+---------------------------+
1 row in set (0.00 sec)

 

Week(date)           返回日期date為一年中的第幾周

mysql> select week("2018-03-04 03:40:20");
+-----------------------------+
| week("2018-03-04 03:40:20") |
+-----------------------------+
|                           9 |
+-----------------------------+
1 row in set (0.00 sec)

 

Year(date)             返回日期date的年份

mysql> select year("2018-03-04 03:40:20");
+-----------------------------+
| year("2018-03-04 03:40:20") |
+-----------------------------+
|                        2018 |
+-----------------------------+
1 row in set (0.00 sec)

 

Datediff(expr,expr2)    返回起始時間expr和結束時間expr2間天數

mysql> select Datediff("2018-03-04 03:40:20","2018-03-05 03:40:20");
+-------------------------------------------------------+
| Datediff("2018-03-04 03:40:20","2018-03-05 03:40:20") |
+-------------------------------------------------------+
|                                                    -1 |
+-------------------------------------------------------+
1 row in set (0.00 sec)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM