MySql教程

mysql day6 第七章数值函数

本文主要是介绍mysql day6 第七章数值函数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
-- 第七章 数值函数
-- select round(5.7345,2); -- 5.73
-- select truncate(5.7345,3); -- 5.734 截断,只保留前三位
-- select ceiling(5.7); -- 6 ,5.2也得6 返回大于或等于这个数字的最小整数
-- select floor(5.2); -- 5 返回小于或等于这个数字的最大整数
-- select abs(-5.2); -- 5.2 绝对值
-- select rand(); -- 0-1之间的随机值

-- -- 字符串函数
-- select length('sky'); -- 3个字符
-- select upper('sky'); -- SKY大写 lower()

-- -- 删除字符串中不需要的空格
-- select ltrim('  sky'); -- 左修正,移除字符串左侧的空白字符或其他预定义字符,同理rtrim/右侧,trim/前导或尾随
-- select left('woshishabi',4); -- 返回字符串左侧的几个字符,同理right()
-- select substring('woshishabi',3,5); -- shish 参数2起始位置 参数3长度(可写可不写)
-- select substring('woshishabi',3);-- shishabi,3开始到结尾
-- select locate('n','woshishabi');-- 3 返回位置,大小写结果相同,若查找的字母不在字符串里,返回0
-- select locate('shabi','woshishabi');-- 6 返回第一次出现的位置
-- select replace('woshishabi','shabi','laji'); -- woshilaji
-- select concat('nihao','shabi'); -- nihaoshabi 拼接字符串

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

-- 日期函数
-- select now(),curdate(),curtime();
-- select year(now()); -- 2021 得到年份 month day hour second minute 整数 dayname monthname 获取字符串格式的星期数
-- select extract(year from now()); -- 把代码录入dbms用extract函数

-- 习题
-- select *
-- from orders
-- where year(order_date) = year(now())-2

-- 格式化日期和时间
-- select date_format(now(),'%y'); -- 21,'%y'表示两位的年份 '%Y'表示四位的年份 ‘%m’表示两位的月份,‘%d’表示两位的日期
-- select time_format(now(),'%H:%i %p') -- 小时 分钟 pm或am 12:46 pm

-- 计算日期和时间
select date_add(now(),interval 1 day);-- 返回明天的同一时间
select date_add(now(),interval 1 year); -- 返回明年的同一时间
select date_add(now(),interval -1 year); -- 返回去年的同一时间 
select date_sub(now(),interval 1 year);-- 返回去年的同一时间 

select datediff('2019-01-04 09:00','2019-01-02 17:00'); -- 2 返回天数的间隔,根据顺序不一样会出现负值
select time_to_sec('09:00'); -- 32400得到从0点开始计算的秒数
select time_to_sec('09:00') - time_to_sec('09:02') -- -120 根据顺序不一样会出现负值

-- ifnull coalesce函数
-- use sql_store;
-- select 
-- 		order_id,
-- 		-- ifnull(shipper_id,'not assigned') as shipper -- 用其他内容代替空值
--         coalesce(shipper_id,comments,'not assigned') as shipper -- 返回第一个非空值
-- from orders

-- 习题
-- use sql_store;
-- select 
-- concat(first_name,' ',last_name) as customer,
-- ifnull(phone,'unknown') as phone
-- from customers

-- if函数
-- 条件为真返回第一个值,否则返回第二个值,只允许单一表达式
-- select 
-- order_id,
-- order_date,
-- 			if(
--             year(order_date) = year(now()),
-- 			'active',
-- 			'archived') as catagory
-- from orders

-- 练习
-- select
-- 		product_id,
-- 		name,
-- 		count(*) as orders, -- 聚合函数计数
-- 		if(
--         count(*) > 1,
-- 		'manytimes',
-- 		'once') as frequency
-- from products
-- join order_items oi using (product_id)
-- group by product_id,name

-- case运算符
-- select
-- 	order_id,
-- 	case
-- 			when year(order_date) = year(now()) then 'active'
-- 			when year(order_date) = year(now()) - 1 then 'last year'
-- 			when year(order_date) < year(now()) - 1 then 'archived'
-- 			else 'future'
-- 	end as category
-- from orders

-- 习题
-- select 
-- concat(first_name,' ',last_name) as customer,
-- points,
-- case 
-- when points > '3000' then 'gold'
-- when points between '2000' and '3000' then 'silver'
-- when points < '2000' then 'bronze'
-- end as category
-- from customers
-- order by points desc
这篇关于mysql day6 第七章数值函数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!