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

数值函数

round():四舍五入

select round(5.37)

运行结果
在这里插入图片描述

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

运行结果
在这里插入图片描述

ceiling():上限函数

select ceiling(5.1)

运行结果
在这里插入图片描述

floor():地板函数

select floor(5.8);

运行结果
在这里插入图片描述

abs():计算绝对值

select abs(-3.5);

运行结果
在这里插入图片描述

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

select rand()

运行结果
在这里插入图片描述

字符串函数

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

select length('xuwuuuu')

运行结果
在这里插入图片描述

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

select upper('xuwuuuu');

运行结果
在这里插入图片描述

select lower('XUWUUUU')

运行结果
在这里插入图片描述

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

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

在这里插入图片描述

select rtrim('xuwuuu   ');

在这里插入图片描述

select trim('  xuwuuu  ')

在这里插入图片描述

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

select left('xuwuuuuu',3)

在这里插入图片描述

select substr('xuwuuuuu',3,2);

在这里插入图片描述

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

select locate('u', 'xuwuuuu')

在这里插入图片描述

select locate('y', 'xuwuuuu')

在这里插入图片描述

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

在这里插入图片描述

replace替换字符或字符串

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

在这里插入图片描述

select replace('xuwuuuuu', 'xu', 's')

在这里插入图片描述

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

在这里插入图片描述

concat:串联两个字符串

select concat('xu', 'wuuuu')

在这里插入图片描述

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

在这里插入图片描述

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

日期函数

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

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

在这里插入图片描述

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

select year(now()), month(now()), day(now()),
       minute(now()), second(now())

在这里插入图片描述

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

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

在这里插入图片描述

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

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

在这里插入图片描述

练习1

use sql_store;
select *
from orders
where year(order_date) = year(now())

date_format():日期格式函数

select date_format(now(), '%M %d %Y')

在这里插入图片描述

time_format():时间格式函数

select time_format(curtime(), '%H:%i %p')

在这里插入图片描述

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

select date_add(now(), interval 1 day),
       date_add(now(), interval -3 year)

返回了明天的同一时间(写码当前是2023-11-29),返回3年后的同一时间
在这里插入图片描述

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

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

在这里插入图片描述

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

select datediff('2023-11-29 22:02', '2022-11-29 21:02'),
       datediff('2022-11-29 22:02', '2023-11-29 21:02')

在这里插入图片描述

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

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

在这里插入图片描述

select time_to_sec('22:10') - time_to_sec('22:00')

在这里插入图片描述

其他函数

ifnull()

use sql_store;
select order_id,
       ifnull(shipper_id, 'Not assigned') as shipper
from orders

在这里插入图片描述

coalesce()

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

在这里插入图片描述

练习1

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

在这里插入图片描述

if():单一表达式

select order_id,
       order_date,
       if(year(order_date) = '2019', 'Active', 'Archived') as category
from orders

在这里插入图片描述

练习2

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

在这里插入图片描述

case运算符:多个表达式

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;

练习3

select concat(first_name, ' ', last_name) as customer,
       points,
       case
           when points &gt; '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 &gt; '3000' then 'Gold'
           when points &gt;= '2000' then 'Silver'
           else 'Bronze'
           end as category
from customers
order by points desc;

在这里插入图片描述

原文地址:https://blog.csdn.net/xuwuuu/article/details/134687015

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_#ID#.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注