mysql数据库管理-函数与运算符

 

1 mysql运算符

满足返回1,不满足返回0,  比较运算符

下表显示常用的mysql常用运算符。

mysql数据库管理-函数与运算符_第1张图片

mysql> select'var' regexp 'r' a;
+---+
| a |
+---+
| 1 |
+---+
1 row in set (0.00 sec)

mysql> select '123' is null ;
+---------------+
| '123' is null |
+---------------+
|             0 |
+---------------+
1 row in set (0.00 sec)

mysql> select 'var' regexp '^v' ;
+-------------------+
| 'var' regexp '^v' |
+-------------------+
|                 1 |
+-------------------+
1 row in set (0.00 sec)

mysql> select 'var' regexp 'v$' ;
+-------------------+
| 'var' regexp 'v$' |
+-------------------+
|                 0 |
+-------------------+
1 row in set (0.00 sec)

mysql> select 'var' regexp '[a-z]';
+----------------------+
| 'var' regexp '[a-z]' |
+----------------------+
|                    1 |
+----------------------+
1 row in set (0.00 sec)

mysql> select 'var' regexp '[a-c]';
+----------------------+
| 'var' regexp '[a-c]' |
+----------------------+
|                    1 |
+----------------------+
1 row in set (0.00 sec)

mysql> select 'd' regexp '[a-c]'
    -> ;

+--------------------+
| 'd' regexp '[a-c]' |
+--------------------+
|                  0 |
+--------------------+
1 row in set (0.00 sec)

mysql> 

mysql> select 'car' regexp '.ar';
+--------------------+
| 'car' regexp '.ar' |
+--------------------+
|                  1 |
+--------------------+
1 row in set (0.00 sec)

mysql> select 'car' regexp '.ra';
+--------------------+
| 'car' regexp '.ra' |
+--------------------+
|                  0 |
+--------------------+
1 row in set (0.00 sec)

mysql> 

其他判断字符运算

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

mysql> select not 1=1;
+---------+
| not 1=1 |
+---------+
|       0 |
+---------+
1 row in set (0.00 sec)

mysql> 

mysql> select not 0   not逻辑符
    -> ;
+-------+
| not 0 |
+-------+
|     1 |
+-------+
1 row in set (0.00 sec)

mysql> select not 5;
+-------+
| not 5 |
+-------+
|     0 |
+-------+
1 row in set (0.00 sec)

mysql> select not -2;
+--------+
| not -2 |
+--------+
|      0 |
+--------+
1 row in set (0.04 sec)

mysql> select 1 and 0,1 and 1 , 1 && null ;   and &&逻辑符
+---------+---------+------------+
| 1 and 0 | 1 and 1 | 1 and null |
+---------+---------+------------+
|       0 |       1 |       NULL |
+---------+---------+------------+
1 row in set (0.00 sec)

mysql> 

mysql> select 1 ||1,1 or 1 ,0 or 0 ;   or ||逻辑符
+-------+--------+--------+
| 1 ||1 | 1 or 1 | 0 or 0 |
+-------+--------+--------+
|     1 |      1 |      0 |
+-------+--------+--------+
1 row in set (0.00 sec)

mysql> 

 

mysql> select 1 xor 1,1 xor 0, 0 xor 0 ;   xor逻辑符
+---------+---------+---------+
| 1 xor 1 | 1 xor 0 | 0 xor 0 |
+---------+---------+---------+
|       0 |       1 |       0 |
+---------+---------+---------+
1 row in set (0.00 sec)

2 MYSQL函数

2.1 数值函数

mysql> select mod(2,3),mod(2,2),mod(3,2);    ---mod求余函数。
+----------+----------+----------+
| mod(2,3) | mod(2,2) | mod(3,2) |
+----------+----------+----------+
|        2 |        0 |        1 |
+----------+----------+----------+
1 row in set (0.07 sec)

mysql> select ceil(2),ceil(2.1),ceiling(2.2);     --ceil大于等于参数的最小整数。
+---------+-----------+-----------+
| ceil(2) | ceil(2.1) | ceil(2.2) |
+---------+-----------+-----------+
|       2 |         3 |         3 |
+---------+-----------+-----------+
1 row in set (0.03 sec)

mysql> select floor(2),floor(2.1),floor(2.2);  --小于等于参数的最大整数
+----------+------------+------------+
| floor(2) | floor(2.1) | floor(2.2) |
+----------+------------+------------+
|        2 |          2 |          2 |
+----------+------------+------------+
1 row in set (0.04 sec)

mysql> 

mysql> select rand(1),rand(100),rand(-100);   --rand函数取小于1的随机数
+---------------------+---------------------+---------------------+
| rand(1)             | rand(100)           | rand(-100)          |
+---------------------+---------------------+---------------------+
| 0.40540353712197724 | 0.17353134804734155 | 0.13690951106782026 |
+---------------------+---------------------+---------------------+
mysql> select ROUND(rand(1),1),ROUND(rand(100),1),ROUND(rand(-100),1);--round函数截取小数点位数,四舍五入
+------------------+--------------------+---------------------+
| ROUND(rand(1),1) | ROUND(rand(100),1) | ROUND(rand(-100),1) |
+------------------+--------------------+---------------------+
|              0.4 |                0.2 |                 0.1 |
+------------------+--------------------+---------------------+
1 row in set (0.01 sec)

mysql> select truncate(1.2,1),truncate(1254.34,-2);  --truncate函数截取小数点位数,不四舍五入
+------------------+--------------------+---------------------+
+-----------------+----------------------+
| truncate(1.2,1) | truncate(1254.34,-2) |
+-----------------+----------------------+
|             1.2 |                 1200 |
+-----------------+----------------------+
1 row in set (0.04 sec)

mysql> 

mysql> select pow(2,3),pow(-2,4),pow(2,-3) --pow或者power函数求平幂数
    -> 
    -> ;
+----------+-----------+-----------+
| pow(2,3) | pow(-2,4) | pow(2,-3) |
+----------+-----------+-----------+
|        8 |        16 |     0.125 |
+----------+-----------+-----------+
1 row in set (0.00 sec)

mysql> 

2.2 字符串函数

常用的字符串函数如下表

 

mysql数据库管理-函数与运算符_第2张图片

mysql数据库管理-函数与运算符_第3张图片

mysql数据库管理-函数与运算符_第4张图片

mysql数据库管理-函数与运算符_第5张图片

mysql> select char_length('clg'),length('欧'),char_length('哦');  --length,char_length求字符长度
+--------------------+--------------+-------------------+
| char_length('clg') | length('欧')  | char_length('哦')  |
+--------------------+--------------+-------------------+
|                  3 |            2 |                 1 |
+--------------------+--------------+-------------------+
1 row in set (0.04 sec)
 

mysql> select concat('1111111','123','111111111','11111111111111111111');----合并变量。
+------------------------------------------------------------+
| concat('1111111','123','111111111','11111111111111111111') |
+------------------------------------------------------------+
| 111111112311111111111111111111111111111                    |
+------------------------------------------------------------+
1 row in set (0.00 sec)

替换与截取函数


mysql> select concat_ws(',','123','222222','11111111111','333333333333') ;--第一个参数为其他参数的分隔符
+------------------------------------------------------------+
| concat_ws(',','123','222222','11111111111','333333333333') |
+------------------------------------------------------------+
| 123,222222,11111111111,333333333333                        |
+------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

mysql> select insert('cccccccc',2,'c','aaaaaa');--字符替代函数,类似oracle的replace函数(第2次出现的c用aaaaaa替代)
+-----------------------------------+
| insert('cccccccc',2,'c','aaaaaa') |
+-----------------------------------+
| caaaaaaccccccc                    |
+-----------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> select insert('cccccccc',2,4,'aaaaaa');  从2开始长度为4的字符替换为aaaaaa。
+---------------------------------+
| insert('cccccccc',2,4,'aaaaaa') |
+---------------------------------+
| caaaaaaccc                      |
+---------------------------------+
1 row in set (0.00 sec)

left和right函数

mysql> select left('ac',2);
+--------------+
| left('ac',2) |
+--------------+
| ac           |
+--------------+
1 row in set (0.00 sec)

mysql> select right('ac',1);
+---------------+
| right('ac',1) |
+---------------+
| c             |
+---------------+
1 row in set (0.00 sec)

mysql>

 

 

mysql> select greatest(1,2,3);
+-----------------+
| greatest(1,2,3) |
+-----------------+
|               3 |
+-----------------+
1 row in set (0.07 sec)

mysql> 
mysql> 
mysql> select least(1,2,3) ;
+--------------+
| least(1,2,3) |
+--------------+
|            1 |
+--------------+
1 row in set (0.08 sec)


mysql> select least(null,2,3) ;
+-----------------+
| least(null,2,3) |
+-----------------+
|            NULL |
+-----------------+
1 row in set (0.02 sec)

mysql> select greatest(null,2,3);
+--------------------+
| greatest(null,2,3) |
+--------------------+
|               NULL |
+--------------------+
1 row in set (0.05 sec)

mysql> 

mysql> select if(id>1,'123','231') from monidb.t;---类似oracle的case when和decode函数。
+----------------------+
| if(id>1,'123','231') |
+----------------------+
| 231                  |
| 123                  |
+----------------------+
2 rows in set (0.06 sec)


mysql> select repeat('mysql',2);
+-------------------+
| repeat('mysql',2) |
+-------------------+
| mysqlmysql        |
+-------------------+
1 row in set (0.05 sec)

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

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

mysql> select trim('   123    ');
+--------------------+
| trim('   123    ') |
+--------------------+
| 123                |
+--------------------+
1 row in set (0.01 sec)


mysql> select substring('12dwadwadafa',1,2);
+-------------------------------+
| substring('12dwadwadafa',1,2) |
+-------------------------------+
| 12                            |
+-------------------------------+
1 row in set (0.00 sec)


mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.4089876305395622 |
+--------------------+
1 row in set (0.01 sec)

mysql> 


mysql> select case when 1=2 then 'clg' when 1=1 then 'ok' else 'what?' end ;  --case when函数
+--------------------------------------------------------------+
| case when 1=2 then 'clg' when 1=1 then 'ok' else 'what?' end |
+--------------------------------------------------------------+
| ok                                                           |
+--------------------------------------------------------------+
1 row in set (0.09 sec)


2.3 日期函数 

常用的日期函数如下表

 

mysql数据库管理-函数与运算符_第6张图片

mysql数据库管理-函数与运算符_第7张图片

mysql数据库管理-函数与运算符_第8张图片

mysql数据库管理-函数与运算符_第9张图片

mysql> select curdate();   --当前的年月日
+------------+
| curdate()  |
+------------+
| 2019-10-25 |
+------------+
1 row in set (0.00 sec)

mysql> select curtime();  -当前的时分秒
+-----------+
| curtime() |
+-----------+
| 01:55:00  |
+-----------+
1 row in set (0.00 sec)

mysql> select now();  ---当前整时间
+---------------------+
| now()               |
+---------------------+
| 2019-10-25 01:55:11 |
+---------------------+
1 row in set (0.00 sec)

mysql> select year(curdate());  --截取年份
+-----------------+
| year(curdate()) |
+-----------------+
|            2019 |
+-----------------+
1 row in set (0.05 sec)

mysql> select month(now());  --截取月
+--------------+
| month(now()) |
+--------------+
|           11 |
+--------------+
1 row in set (0.00 sec)

mysql> select day(now());-- 截取日
+------------+
| day(now()) |
+------------+
|         27 |
+------------+
1 row in set (0.01 sec)

mysql> select min(curtime()); --截取时分秒
+----------------+
| min(curtime()) |
+----------------+
| 02:51:12       |
+----------------+
1 row in set (0.01 sec)


mysql> select hour(curtime());--截取时
+-----------------+
| hour(curtime()) |
+-----------------+
|               2 |
+-----------------+
1 row in set (0.00 sec)

mysql> select minute(curtime());---截取分
+-------------------+
| minute(curtime()) |
+-------------------+
|                51 |
+-------------------+
1 row in set (0.00 sec)

 

mysql> select date_add(now(),interval 1 day)    --- mysql的sysdate+1/1440
    -> ;
+--------------------------------+
| date_add(now(),interval 1 day) |
+--------------------------------+
| 2019-11-28 20:02:05            |
+--------------------------------+
1 row in set (0.05 sec)

mysql> select adddate(now(),interval 1 day);
+-------------------------------+
| adddate(now(),interval 1 day) |
+-------------------------------+
| 2019-11-28 20:03:23           |
+-------------------------------+
1 row in set (0.02 sec)

mysql> select adddate(now(),interval 1 second);
+----------------------------------+
| adddate(now(),interval 1 second) |
+----------------------------------+
| 2019-11-27 20:03:31              |
+----------------------------------+
1 row in set (0.02 sec)

mysql> select adddate(now(),interval 1 minute);
+----------------------------------+
| adddate(now(),interval 1 minute) |
+----------------------------------+
| 2019-11-27 20:04:37              |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select adddate(now(),interval 1 hour);
+--------------------------------+
| adddate(now(),interval 1 hour) |
+--------------------------------+
| 2019-11-27 21:03:43            |
+--------------------------------+
1 row in set (0.00 sec)

mysql> 

 

mysql> select subdate(now(),interval 1 year);   ------相当于sysdate-1/1440,DATE_SUB()
+--------------------------------+
| subdate(now(),interval 1 year) |
+--------------------------------+
| 2018-11-27 20:05:10            |
+--------------------------------+
1 row in set (0.00 sec)

mysql> select subdate(now(),interval 1 month);
+---------------------------------+
| subdate(now(),interval 1 month) |
+---------------------------------+
| 2019-10-27 20:05:16             |
+---------------------------------+
1 row in set (0.00 sec)

mysql> select subdate(now(),interval 1 day);
+-------------------------------+
| subdate(now(),interval 1 day) |
+-------------------------------+
| 2019-11-26 20:05:24           |
+-------------------------------+
1 row in set (0.00 sec)

mysql> select subdate(now(),interval 1 hour);
+--------------------------------+
| subdate(now(),interval 1 hour) |
+--------------------------------+
| 2019-11-27 19:05:29            |
+--------------------------------+
1 row in set (0.00 sec)

mysql> select subdate(now(),interval 1 second);
+----------------------------------+
| subdate(now(),interval 1 second) |
+----------------------------------+
| 2019-11-27 20:05:32              |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select subdate(now(),interval 1 minute);
+----------------------------------+
| subdate(now(),interval 1 minute) |
+----------------------------------+
| 2019-11-27 20:04:39              |
+----------------------------------+
1 row in set (0.00 sec)

mysql> 

mysql时间与字符串之间相互转换

1.时间转字符串

DATE_FORMAT(日期,格式字符串)

SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s');

mysql> SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s');
+-----------------------------------------+
| DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') |
+-----------------------------------------+
| 2019-11-27 20:14:06                     |
+-----------------------------------------+
1 row in set (0.08 sec)
 

2.字符串转时间

STR_TO_DATE(字符串,日志格式)

SELECT STR_TO_DATE('2019-01-20 16:01:45', '%Y-%m-%d %H:%i:%s');

3.时间转时间戳

select unix_timestamp(now());

4.字符串转时间戳

select unix_timestamp('2019-01-20');    ---1574914481

5.时间戳转字符串

select from_unixtime(1451997924,'%Y-%d');

附日期格式如下:

%M 月名字(January……December)  
%W 星期名字(Sunday……Saturday)  
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)  
%Y 年, 数字, 4 位  
%y 年, 数字, 2 位  
%a 缩写的星期名字(Sun……Sat)  
%d 月份中的天数, 数字(00……31)  
%e 月份中的天数, 数字(0……31)  
%m 月, 数字(01……12)  
%c 月, 数字(1……12)  
%b 缩写的月份名字(Jan……Dec)  
%j 一年中的天数(001……366)  
%H 小时(00……23)  
%k 小时(0……23)  
%h 小时(01……12)  
%I 小时(01……12)  
%l 小时(1……12)  
%i 分钟, 数字(00……59)  
%r 时间,12 小时(hh:mm:ss [AP]M)  
%T 时间,24 小时(hh:mm:ss)  
%S 秒(00……59)  
%s 秒(00……59)  
%p AM或PM  
%w 一个星期中的天数(0=Sunday ……6=Saturday )  
%U 星期(0……52), 这里星期天是星期的第一天  
%u 星期(0……52), 这里星期一是星期的第一 

你可能感兴趣的:(MYSQL基础管理)