mysql 06內置函數——個人筆記

mysql 內置函數的

  • 慎用,慎用,慎用

字符串

select 的主要功能其實是打印

1
concat()	拼接字符串
select concat("我","你") as 拼接;
+------+
| 拼接 |
+------+
| 我你 |
+------+
2
length(str) 獲取字符串長度

select length("夜晚有多長") as 今夜;
+------+
| 今夜 |
+------+
|   10 |
+------+

3
lower(str)所有字符串小寫

select lower("TO THE MOON") as for_river ;
+-------------+
| for_river   |
+-------------+
| to the moon |
+-------------+


4
upper(str)所有字符串大寫

 select upper("little dog") as 星座;
+------------+
| 星座       |
+------------+
| LITTLE DOG |
+------------+

5
left(str,x) 返回字符串str最左邊的x個字符串

select left("涉江採芙蓉",2);
+----------------------+
| left("涉江採芙蓉",2) |
+----------------------+
| 涉江                 |
+----------------------+

6
right(str,x)

select right("涉江採芙蓉",2);
+-----------------------+
| right("涉江採芙蓉",2) |
+-----------------------+
| 芙蓉                  |
+-----------------------+


7
lpad(str,n,pad) 用字符串pad對str最右邊填充直到長度爲n

mysql> select lpad("你是誰",5,"誰");
+-----------------------+
| lpad("你是誰",5,"誰") |
+-----------------------+
| 誰誰你是誰            |
+-----------------------+

select rpad("你是誰",5,"誰");
+-----------------------+
| rpad("你是誰",5,"誰") |
+-----------------------+
| 你是誰誰誰            |
+-----------------------+


8
ltrim 去除最左邊空格

mysql> select ltrim('          haha');
+-------------------------+
| ltrim('          haha') |
+-------------------------+
| haha                    |
+-------------------------+
1 row in set (0.00 sec)

9
right 去除右邊空格

mysql> select concat(rtrim('          haha       '),'world'); #去除右側空格
+------------------------------------------------+
| concat(rtrim('          haha       '),'world') |
+------------------------------------------------+
|           hahaworld                            |
+------------------------------------------------+

10

trim() 去除兩邊空格
 select trim("    fds   ");
+--------------------+
| trim("    fds   ") |
+--------------------+
| fds                |
+--------------------+


11
repeat(str,x)返回字符串中出現x次的結果

12
replace (str,a,b)將字符串中的a替換成b

13
strcmp(s1,s2)
比較兩個字符串 完全一樣返回0 s1的首字母比s2的首字母大返回1 否則返回-1

14
mysql> select substr("python is so good",1,5); #從下標1開始截取5個 
+---------------------------------+
| substr("python is so good",1,5) |
+---------------------------------+
| pytho                           |
+---------------------------------+

數學

1. bin()  # 10進制轉2進制  
mysql> select bin(99);                                                                                                          
+---------+                                                                                                                     
| bin(99) |                                                                                                                     
+---------+                                                                                                                     
| 1100011 |                                                                                                                     
+---------+


2. ceiling() # 向上取整  
mysql> select ceiling(13.1);                                                                                                    
+---------------+                                                                                                               
| ceiling(13.1) |                                                                                                               
+---------------+                                                                                                               
|            14 |                                                                                                               
+---------------+



3. floor() #向下取整
mysql> select floor(13.1);                                                                                                      
+-------------+                                                                                                                 
| floor(13.1) |                                                                                                                 
+-------------+                                                                                                                 
|          13 |                                                                                                                 
+-------------+

4. max()


5. min()


6. sqrt()
mysql> select sqrt(10);                                                                                                         
+--------------------+                                                                                                          
| sqrt(10)           |                                                                                                          
+--------------------+                                                                                                          
| 3.1622776601683795 |                                                                                                          
+--------------------+

7. rand()
mysql> select rand();                                                                                                           
+--------------------+                                                                                                          
| rand()             |                                                                                                          
+--------------------+                                                                                                          
| 0.3009522643880474 |                                                                                                          
+--------------------+  


日期

1
now() #當前的日期加時間 
2
year(date) # date日期坐在的年份
3
month(date) # date日期坐在的月份
4
week(date) # date日期坐在的第幾周 
5
unix_timestamp(date) #日期所在的時間戳
6
from_unixtime(時間戳) #時間戳轉日期
7
mysql> select datediff('1998-10-20','2015-7-23');  #日期差額


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章