文章目录

  • 数值函数
  • round():四舍五入
  • ceiling():上限函数
  • floor():地板函数
  • abs():计算绝对值
  • rand():生成0-1的随机浮点数
  • 字符串函数
  • length():获取字符串中的字符数
  • upper() / lower():将字符串转化成大小写
  • trim/ltrim/rtrim:删除字符串中不需要的空格
  • left /right /substr:返回字符串相应位置字符
  • locate:返回第一个字符或一串字符匹配位置
  • replace:替换字符或字符串
  • concat:串联两个字符串
  • 日期函数
  • now() / curdate() / curtime():返回当前的日期和时间 / 返回当前日期 / 返回当前时间
  • year(now()) / month() / day() / hour() / minute() / second() 提取特定日期或时间
  • dayname(now()) / monthname():返回字符串类型的星期和月份
  • extract(day from now()):返回指定的时间或日期
  • 练习1
  • date_format():日期格式函数
  • time_format():时间格式函数
  • date_add():给日期时间值增加日期,写负值也能减
  • date_sub():给日期时间值减去日期,写负值也能加
  • datediff():返回两个日期的天数间隔
  • time_to_sec():返回从零点计算的秒数
  • 其他函数
  • ifnull()
  • coalesce()
  • 练习1
  • if():单一表达式
  • 练习2
  • case运算符:多个表达式
  • 练习3


数值函数

round():四舍五入

  • 参数1
select round(5.37)

运行结果

mysql时间类型转换为字符串类型 mysql时间转字符串函数_数据库

  • 参数2,可指定四舍五入的精度
select round(5.37,1)

运行结果

mysql时间类型转换为字符串类型 mysql时间转字符串函数_后端_02

ceiling():上限函数

  • 返回大于或等于这个数字的最小整数
select ceiling(5.1)

运行结果

mysql时间类型转换为字符串类型 mysql时间转字符串函数_mysql时间类型转换为字符串类型_03

floor():地板函数

  • 返回小于或等于这个数字的最大整数
select floor(5.8);

运行结果

mysql时间类型转换为字符串类型 mysql时间转字符串函数_mysql时间类型转换为字符串类型_04

abs():计算绝对值

select abs(-3.5);

运行结果

mysql时间类型转换为字符串类型 mysql时间转字符串函数_后端_05

rand():生成0-1的随机浮点数

select rand()

运行结果

mysql时间类型转换为字符串类型 mysql时间转字符串函数_sql_06

字符串函数

length():获取字符串中的字符数

select length('xuwuuuu')

运行结果

mysql时间类型转换为字符串类型 mysql时间转字符串函数_sql_07

upper() / lower():将字符串转化成大小写

select upper('xuwuuuu');

运行结果

mysql时间类型转换为字符串类型 mysql时间转字符串函数_mysql_08

select lower('XUWUUUU')

运行结果

mysql时间类型转换为字符串类型 mysql时间转字符串函数_sql_09

trim/ltrim/rtrim:删除字符串中不需要的空格

  • ltrim():移除字符串左侧的空白字符或其他预定义字符
select ltrim('   xuwuuu')

mysql时间类型转换为字符串类型 mysql时间转字符串函数_后端_10

  • rtirm():移除字符串右侧的空白字符或其他预定义字符
select rtrim('xuwuuu   ');

mysql时间类型转换为字符串类型 mysql时间转字符串函数_mysql时间类型转换为字符串类型_11

  • trim():删除所有前导或尾随空格,但不能删除字符串中间的空格
select trim('  xuwuuu  ')

mysql时间类型转换为字符串类型 mysql时间转字符串函数_mysql_12

left /right /substr:返回字符串相应位置字符

  • left():返回字符串左侧的几个字符
select left('xuwuuuuu',3)

mysql时间类型转换为字符串类型 mysql时间转字符串函数_后端_13

  • right():返回字符串右侧的几个字符
  • substr():字符截取函数,可以得到任何位置的字符;
  • 第二个参数是起始位置,第三个参数是长度;
  • 也可以不写第三个参数,这样就返回从起始位置到字符串最后的所有字符。
select substr('xuwuuuuu',3,2);

mysql时间类型转换为字符串类型 mysql时间转字符串函数_后端_14

locate:返回第一个字符或一串字符匹配位置

  • 这个函数不区分大小写
  • 返回指定的字符在字符串中第一次出现的位置。
select locate('u', 'xuwuuuu')

mysql时间类型转换为字符串类型 mysql时间转字符串函数_后端_15

  • 如果指定的字符在字符串中没有,会返回0
select locate('y', 'xuwuuuu')

mysql时间类型转换为字符串类型 mysql时间转字符串函数_后端_16

  • 还可以搜索一串字符串
  • 同样,要搜索的字符串不存在时,也会返回0
select locate('uuuu', 'xuwuuuu')

mysql时间类型转换为字符串类型 mysql时间转字符串函数_sql_17

replace:替换字符或字符串

  • 第一个参数:完整的字符串;第二个参数:要被替换掉的字符或字符串;第三个参数:新的字符或字符串
select replace('xuwuuuuu', 'xu', 'st')

mysql时间类型转换为字符串类型 mysql时间转字符串函数_数据库_18

  • 一个字符替换一个字符串也是可以的,一个字符串替换一个字符也可以
select replace('xuwuuuuu', 'xu', 's')

mysql时间类型转换为字符串类型 mysql时间转字符串函数_sql_19

select replace('xuwuuuuu', 'x', 'st')

mysql时间类型转换为字符串类型 mysql时间转字符串函数_后端_20

concat:串联两个字符串

select concat('xu', 'wuuuu')

mysql时间类型转换为字符串类型 mysql时间转字符串函数_sql_21

  • 练习:把表中的名和姓串联起来

mysql时间类型转换为字符串类型 mysql时间转字符串函数_mysql_22

use sql_store;
select concat(first_name, ' ', last_name) as full_name
from customers

日期函数

now() / curdate() / curtime():返回当前的日期和时间 / 返回当前日期 / 返回当前时间

select now(), curdate(), curtime();

mysql时间类型转换为字符串类型 mysql时间转字符串函数_sql_23

year(now()) / month() / day() / hour() / minute() / second() 提取特定日期或时间

  • 返回整数值
select year(now()), month(now()), day(now()),
       minute(now()), second(now())

mysql时间类型转换为字符串类型 mysql时间转字符串函数_数据库_24

dayname(now()) / monthname():返回字符串类型的星期和月份

  • 返回字符串
select dayname(now()), monthname(now())

mysql时间类型转换为字符串类型 mysql时间转字符串函数_后端_25

extract(day from now()):返回指定的时间或日期

  • 要定制单位,如年、月、日期、秒等
select extract(day from now()),
       extract(year from now()),
       extract(month from now())

mysql时间类型转换为字符串类型 mysql时间转字符串函数_sql_26

练习1

  • 筛选当前年份的订单。把当前时间的年份提取出来,再把订单日期的年份提出来,筛选两个年份相等的数据。
use sql_store;
select *
from orders
where year(order_date) = year(now())

date_format():日期格式函数

  • %y:两位数的年份
  • %Y:四位数的年份
  • %m:两位数的月份
  • %M:字符串的月份
  • %d:获取当前日期
select date_format(now(), '%M %d %Y')

mysql时间类型转换为字符串类型 mysql时间转字符串函数_sql_28

time_format():时间格式函数

  • 两个参数;一个是时间值,一个是格式字符串。
select time_format(curtime(), '%H:%i %p')

mysql时间类型转换为字符串类型 mysql时间转字符串函数_后端_29

date_add():给日期时间值增加日期,写负值也能减

  • 想在当前日期时间上增加一天
  • 第二个参数
  • interval 1 day;
  • interval 3 year;
  • interval -1 year;获取过去的时间
select date_add(now(), interval 1 day),
       date_add(now(), interval -3 year)

返回了明天的同一时间(写码当前是2023-11-29),返回3年后的同一时间

mysql时间类型转换为字符串类型 mysql时间转字符串函数_mysql_30

date_sub():给日期时间值减去日期,写负值也能加

select date_sub(now(), interval 4 day),
       date_sub(now(), interval -3 year)

mysql时间类型转换为字符串类型 mysql时间转字符串函数_sql_31

datediff():返回两个日期的天数间隔

  • 只返回天数的间隔,不考虑时间的间隔
  • 两个时间的顺序调换,会得到正值或负值
select datediff('2023-11-29 22:02', '2022-11-29 21:02'),
       datediff('2022-11-29 22:02', '2023-11-29 21:02')

mysql时间类型转换为字符串类型 mysql时间转字符串函数_mysql时间类型转换为字符串类型_32

time_to_sec():返回从零点计算的秒数

  • 返回从零点计算的秒数
select time_to_sec('00:30'),
       time_to_sec(curtime())

mysql时间类型转换为字符串类型 mysql时间转字符串函数_sql_33

  • 返回两个时间间隔的秒数
select time_to_sec('22:10') - time_to_sec('22:00')

mysql时间类型转换为字符串类型 mysql时间转字符串函数_后端_34

其他函数

ifnull()

  • 有些订单的发货id是空值。如果想让用户看到的是“未分配”而不是空值,可以调用ifnull()函数,把发货id替换成别的。
use sql_store;
select order_id,
       ifnull(shipper_id, 'Not assigned') as shipper
from orders

mysql时间类型转换为字符串类型 mysql时间转字符串函数_数据库_35

coalesce()

  • 如果参数1是null,就返回参数2;参数2为空,就返回参数3
  • 写一堆参数,coalesce函数会返回这堆参数中第一个非空值。
use sql_store;
select order_id,
       coalesce(shipper_id, comments, 'Not assigned') as shipper
from orders

mysql时间类型转换为字符串类型 mysql时间转字符串函数_sql_36

练习1

  • 返回客户的姓名和电话,没有电话的显示Unknown
  • concat()函数串联字符串!
select concat(first_name, ' ', last_name) as customer,
       ifnull(phone, 'Unknown')
from customers

mysql时间类型转换为字符串类型 mysql时间转字符串函数_sql_37

if():单一表达式

  • if(expression,first,second):如果表达式为真,则返回first,否则返回second
  • 把订单分类,如果订单是今年的,就放在“活跃类别”;否则就放入“归档类别”
  • 可以用union,筛选活跃类别的,再筛选归档类别的,用union连接起来
  • 可以用if函数,达到同样效果
select order_id,
       order_date,
       if(year(order_date) = '2019', 'Active', 'Archived') as category
from orders

mysql时间类型转换为字符串类型 mysql时间转字符串函数_mysql时间类型转换为字符串类型_38

练习2

  • count(*)按照product_id进行计数,就能得到每个id对应有多少数据。
select product_id,
       name,
       count(*) as orders,
       if(count(*) > 1, 'Many times', 'Once') as frequency
from products
join order_items using (product_id)
group by product_id

mysql时间类型转换为字符串类型 mysql时间转字符串函数_数据库_39

case运算符:多个表达式

  • 在有多个表达式且想针对每个表达式返回不同值时,适用case运算符
  • case
    when …… then
    when …… then
    when …… then
    else
    end as ……
select order_id,
       order_date,
       case
           when year(order_date) = '2019' then 'Active'
           when year(order_date) = '2018' then 'Last Year'
           when year(order_date) < '2018' then 'Archived'
           else 'Future'
       end as category
from orders;

mysql时间类型转换为字符串类型 mysql时间转字符串函数_数据库_40

练习3

  • 按照积分给客户分为3类。
select concat(first_name, ' ', last_name) as customer,
       points,
       case
           when points > '3000' then 'Gold'
           when points between '2000' and '3000' then 'Sliver'
           when points < '2000' then 'Bronze'
       end as category
from customers
order by points desc
select concat(first_name, ' ', last_name) as customer,
       points,
       case
           when points > '3000' then 'Gold'
           when points >= '2000' then 'Silver'
           else 'Bronze'
           end as category
from customers
order by points desc;

mysql时间类型转换为字符串类型 mysql时间转字符串函数_数据库_41