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');  #日期差额


你可能感兴趣的:(mysql,数据库)