[MySQL] Detailed explanation of built-in functions

[MySQL] Detailed explanation of built-in functions

date function

insert image description here

--获取当前日期
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2023-07-09     |
+----------------+
--获取当前时间
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 09:42:21       |
+----------------+
--获取当前时间戳
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2023-07-09 09:42:27 |
+---------------------+
--获取当前日期时间
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2023-07-09 09:43:07 |
+---------------------+
--从日期时间中提取日期
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2023-07-09  |
+-------------+
--在当前日期基础上增加10天
mysql> select date_add(now(),interval 10 day);
+---------------------------------+
| date_add(now(),interval 10 day) |
+---------------------------------+
| 2023-07-19 09:52:56             |
+---------------------------------+
--在当前日期基础上减少10天
mysql> select date_sub(now(),interval 10 day);
+---------------------------------+
| date_sub(now(),interval 10 day) |
+---------------------------------+
| 2023-06-29 09:53:04             |
+---------------------------------+
--获取时间差
mysql> select datediff(date(now()),'2002-05-01');
+------------------------------------+
| datediff(date(now()),'2002-05-01') |
+------------------------------------+
|                               7739 |
+------------------------------------+

Comprehensive case

--创建msg表
mysql> create table msg (
id int primary key auto_increment,
content varchar(30) not null,
sendtime datetime
);
--插入两条数据
mysql> insert into msg(content,sendtime) values('天街小雨润如酥', now());
mysql> insert into msg(content,sendtime) values('草色遥看近却无', now());
--显示所有留言信息,发布日期只显示日期,不用显示时间
mysql> select * from msg;
+----+-----------------------+---------------------+
| id | content               | sendtime            |
+----+-----------------------+---------------------+
|  1 | 天街小雨润如酥        | 2023-07-09 10:20:25 |
|  2 | 草色遥看近却无        | 2023-07-09 10:20:36 |
+----+-----------------------+---------------------+

--查询在10分钟内发布的帖子
mysql> select * from msg where date_add(sendtime,interval 10  minute)>now();
+----+-----------------------+---------------------+
| id | content               | sendtime            |
+----+-----------------------+---------------------+
|  1 | 天街小雨润如酥        | 2023-07-09 10:20:25 |
|  2 | 草色遥看近却无        | 2023-07-09 10:20:36 |
+----+-----------------------+---------------------+
mysql> select * from msg where date_sub(now(),interval 10 minute)<sendtime;
+----+-----------------------+---------------------+
| id | content               | sendtime            |
+----+-----------------------+---------------------+
|  1 | 天街小雨润如酥        | 2023-07-09 10:20:25 |
|  2 | 草色遥看近却无        | 2023-07-09 10:20:36 |
+----+-----------------------+---------------------+

insert image description here

string functions

insert image description here

1. Obtain the character set of the content column of the msg table
2. Concatenate and display the information in the stu table
3. Find the number of bytes occupied by the student name in the student table
The length function returns the length of the string in bytes . Multibyte characters count as multiple bytes; single-byte characters count as one byte. For example: letters and numbers are counted as one byte, and Chinese means multiple bytes (related to the character set encoding, utf-8 is three bytes) 4. Replace all math scores in the
stu table with 8 with '6'
5. Intercept the second to third characters of the name field in the stu table
6. Display the names of all employees in lowercase with the first letter

mysql> select charset(content) from msg;
+------------------+
| charset(content) |
+------------------+
| utf8             |
| utf8             |
+------------------+

mysql> select concat('姓名:',name,'总分:',chinese+math+english,'语文',chinese,'数学',math,'英语',english) msg from stu;
+--------------------------------------------------------+
| msg                                                    |
+--------------------------------------------------------+
| 姓名:唐三藏总分:288语文134数学98英语56                |
| 姓名:猪悟能总分:364语文176数学98英语90                |
| 姓名:曹孟德总分:297语文140数学90英语67                |
| 姓名:刘玄德总分:270语文110数学115英语45               |
| 姓名:孙权总分:291语文140数学73英语78                  |
| 姓名:宋公明总分:275语文150数学95英语30                |
+--------------------------------------------------------+

mysql> select name,length(name) from stu;
+-----------+--------------+
| name      | length(name) |
+-----------+--------------+
| 唐三藏    |            9 |
| 猪悟能    |            9 |
| 曹孟德    |            9 |
| 刘玄德    |            9 |
| 孙权      |            6 |
| 宋公明    |            9 |
+-----------+--------------+
mysql> select math,replace(math,'8','6') from stu;
+------+-----------------------+
| math | replace(math,'8','6') |
+------+-----------------------+
|   98 | 96                    |
|   98 | 96                    |
|   90 | 90                    |
|  115 | 115                   |
|   73 | 73                    |
|   95 | 95                    |
+------+-----------------------+
6 rows in set (0.00 sec)
--截取stu表中name字段的第二个到第三个字符
mysql> select name,substring(name,2,2) from stu;
+-----------+---------------------+
| name      | substring(name,2,2) |
+-----------+---------------------+
| 唐三藏    | 三藏                |
| 猪悟能    | 悟能                |
| 曹孟德    | 孟德                |
| 刘玄德    | 玄德                |
| 孙权      ||
| 宋公明    | 公明                |
+-----------+---------------------+
--以首字母小写的方式显示所有员工的姓名
mysql> select ename,concat(lcase(substring(ename,1,1)),substring(ename,2)) from emp;
+--------+--------------------------------------------------------+
| ename  | concat(lcase(substring(ename,1,1)),substring(ename,2)) |
+--------+--------------------------------------------------------+
| SMITH  | sMITH                                                  |
| ALLEN  | aLLEN                                                  |
| WARD   | wARD                                                   |
| JONES  | jONES                                                  |
| MARTIN | mARTIN                                                 |
| BLAKE  | bLAKE                                                  |
| CLARK  | cLARK                                                  |
| SCOTT  | sCOTT                                                  |
| KING   | kING                                                   |
| TURNER | tURNER                                                 |
| ADAMS  | aDAMS                                                  |
| JAMES  | jAMES                                                  |
| FORD   | fORD                                                   |
| MILLER | mILLER                                                 |
+--------+--------------------------------------------------------+

math function

insert image description here

--取绝对值
mysql> select abs(10.2);
+-----------+
| abs(10.2) |
+-----------+
|      10.2 |
+-----------+
--取绝对值
mysql> select abs(-10.5);
+------------+
| abs(-10.5) |
+------------+
|       10.5 |
+------------+
--转为二进制
mysql> select bin(5);
+--------+
| bin(5) |
+--------+
| 101    |
+--------+
--对小数转二进制会自动取整
mysql> select bin(5.2);
+----------+
| bin(5.2) |
+----------+
| 101      |
+----------+

--转化为16进制
mysql> select hex(10);
+---------+
| hex(10) |
+---------+
| A       |
+---------+
--将十进制16转为2进制
mysql> select conv(16,10,2);
+---------------+
| conv(16,10,2) |
+---------------+
| 10000         |
+---------------+

--向上取整
mysql> select ceiling(5.2);
+--------------+
| ceiling(5.2) |
+--------------+
|            6 |
+--------------+
--向上取整
mysql> select ceiling(-5.2);
+---------------+
| ceiling(-5.2) |
+---------------+
|            -5 |
+---------------+
----向下取整
mysql> select floor(-5.2);
+-------------+
| floor(-5.2) |
+-------------+
|          -6 |
+-------------+
----向下取整
mysql> select floor(5.2);
+------------+
| floor(5.2) |
+------------+
|          5 |
+------------+

--保留2位小数位数(小数四舍五入)
mysql> select format(12.3569,2)
    -> ;
+-------------------+
| format(12.3569,2) |
+-------------------+
| 12.36             |
+-------------------+

--生成[0.0,10.0)之间的随机数
mysql> select rand()*10;
+-------------------+
| rand()*10         |
+-------------------+
| 3.120866942332002 |
+-------------------+
mysql> select format(rand()*10,2);
+---------------------+
| format(rand()*10,2) |
+---------------------+
| 8.73                |
+---------------------+
--取模
mysql> select mod(5,2);
+----------+
| mod(5,2) |
+----------+
|        1 |
+----------+

Encryption functions and others

user() Query the current user
md5(str) Digest a string with md5, and get a 32-bit string after the digest
For an encrypted data, if you want to query it, you need to use it together

insert into user (name,password) values ('wmh',md5('123'));
select * from user where password=md5('123');
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
mysql> select md5('root');
+----------------------------------+
| md5('root')                      |
+----------------------------------+
| 63a9f0ea7bb98050796b649e85481845 |
+----------------------------------+
--database()显示当前正在使用的数据库
mysql> select database();
+------------+
| database() |
+------------+
| scott      |
+------------+
--password()函数,MySQL数据库使用该函数对用户加密
mysql> select password('root');
+-------------------------------------------+
| password('root')                          |
+-------------------------------------------+
| *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-------------------------------------------+

--ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值
mysql> select ifnull(null,10);
+-----------------+
| ifnull(null,10) |
+-----------------+
|              10 |
+-----------------+
mysql> select ifnull(20,null);
+-----------------+
| ifnull(20,null) |
+-----------------+
|              20 |
+-----------------+


insert image description here
Problem-solving idea: length(string) counts the length of all strings; minus the length without commas, it is the length of commas, here you need to replace ',' with ''

select id,length(string)-length(replace(string,',','')) from strings;

Guess you like

Origin blog.csdn.net/m0_54469145/article/details/131619708