MySQL(入门篇13)常用的函数和操作

####官网最全的函数总结

一、常用函数

1. 数学运算

运算符 意义 使用
1.abs() 绝对值 abs(-1) 输出 1
2.ceiling() 向上取整 ceiling(9.4) 为 10
3.floor() 向下取整 floor(9.4)为 9
4.rand() 返回一个0-1的随机数字 rand() = 0.2808596950777431
5.sign() 判断数字符号 sign(-2) 为-1,

2.字符串函数

运算符 意义 使用
1.char_length() 字符串长度 char_length(“hello”) 为 5
2. concat() 合并字符串 concat(“he”,“llo”) 为 hello
3. insert() 替换位置字符串 SELECTINSERT(“hello”,2,4,“i”) 为 hi(注意:这里不是索引)
4. replace() 替换指定字符串 replace(“hello”,“ello”,“i”) 为 hi
5. lower() 全部小写 lower(“Hello”) 为 hello
6. upper() 全部大写 lower(“Hello”) 为 HELLO
7. substr() 返回位置的字符串 substr(“hello”,2,3)源字符串,开始位置,长度 返回为 ell
8. instr() 返回开始出现的位置(不是索引!) instr(“hello”,“l”)为3
9. reverse() 反转字符串 reverse(“你好”) 为 好你

3.时间和日期函数

3.1获取时间和日期.

运算符 意义 使用
1. current_time() 当前时间 current_time()为 18:29:54
2. current_date() 当前的日期 current_date() 为 2020-05-24
3. now() 当前时间(等于日期date+时间time) now() 为 2020-05-24 18:33:52
4. localtime() 本地时间(和 now一致) 2020-05-24 18:41:07
5. sysdate() 系统时间(和now()一致) 2020-05-24 18:41:07

获取需要的指定字段

运算符 意义 使用
year(date) 获取日期的年份 year(now()) 为 2020
month(date) 获取月份 month(now() )为 5
day(date) day(now()) 为24
hour() 小时 hour(now()) 为 18
minute() minute(now()) 为39
second() second(now()) 为 42

4.系统函数

运算符 意义 使用
1. system_user() 系统用户 root@localhost
2. user() 用户 root@localhost
3. version() 系统版本 5.7.30
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章