select round(5.37)
select round(5.37,1)
select ceiling(5.1)
select floor(5.8);
select abs(-3.5);
select rand()
select length('xuwuuuu')
select upper('xuwuuuu');
select lower('XUWUUUU')
select ltrim(' xuwuuu')
select rtrim('xuwuuu ');
select trim(' xuwuuu ')
select left('xuwuuuuu',3)
select substr('xuwuuuuu',3,2);
select locate('u', 'xuwuuuu')
select locate('y', 'xuwuuuu')
select locate('uuuu', 'xuwuuuu')
select replace('xuwuuuuu', 'xu', 'st')
select replace('xuwuuuuu', 'xu', 's')
select replace('xuwuuuuu', 'x', 'st')
select concat('xu', 'wuuuu')
use sql_store;
select concat(first_name, ' ', last_name) as full_name
from customers
select now(), curdate(), curtime();
select year(now()), month(now()), day(now()),
minute(now()), second(now())
select dayname(now()), monthname(now())
select extract(day from now()),
extract(year from now()),
extract(month from now())
use sql_store;
select *
from orders
where year(order_date) = year(now())
select date_format(now(), '%M %d %Y')
select time_format(curtime(), '%H:%i %p')
select date_add(now(), interval 1 day),
date_add(now(), interval -3 year)
返回了明天的同一时间(写码当前是2023-11-29),返回3年后的同一时间
select date_sub(now(), interval 4 day),
date_sub(now(), interval -3 year)
select datediff('2023-11-29 22:02', '2022-11-29 21:02'),
datediff('2022-11-29 22:02', '2023-11-29 21:02')
select time_to_sec('00:30'),
time_to_sec(curtime())
select time_to_sec('22:10') - time_to_sec('22:00')
use sql_store;
select order_id,
ifnull(shipper_id, 'Not assigned') as shipper
from orders
use sql_store;
select order_id,
coalesce(shipper_id, comments, 'Not assigned') as shipper
from orders
select concat(first_name, ' ', last_name) as customer,
ifnull(phone, 'Unknown')
from customers
select order_id,
order_date,
if(year(order_date) = '2019', 'Active', 'Archived') as category
from orders
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
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;
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;