#######################
#第10章 创建计算字段
#######################
use test;
select * from vendors;
####拼接字段 concat把多个字符串拼接起来 ,串与串之间用逗号分隔
select concat(vend_name,' (',vend_country, ')') from vendors
order by vend_name;#四部分,vend_name,(,vend_country,)
#RTrim去掉右侧的空格
select concat(rtrim(vend_name),' (',rtrim(vend_country), ')') from vendors
order by vend_name;
#别名 as
select concat(rtrim(vend_name),' (',rtrim(vend_country), ')') as vend_title from vendors
order by vend_name;
####执行算术计算 + _ * /
select prod_id ,quantity,item_price,quantity*item_price as expanded_price from orderitems
where order_num=20005;
###############################
#第十一章 使用数据处理函数
###############################
##文本处理函数
#left length locate lower ltrim right rtrim soundex substring upper
select vend_name,Upper(vend_name) as vend_name_upcase from vendors
order by vend_name;
select cust_name,cust_contact from customers
where cust_contact ='Y. Lie';
select cust_name,cust_contact from customers
where soundex(cust_contact) =soundex('Y. Lie');#模糊语音识别
##日期和时间处理函数 日期格式yyyy-mm-dd
select cust_id,order_num from orders
where Date(order_date)='2005-09-01';#指示数据库只对应日期的部分
select cust_id,order_num from orders
where date(order_date) between '2005-09-01' and '2005-09-30';#寻找一个时间段
select cust_id,order_num from orders
where year(order_date)=2005 and month(order_date)=9;#另一种实现方法
##数值处理函数
#abs cos exp mod pi rand sin sqrt tan
##########################
#第十二章 汇总数据
##########################
##聚集函数 avg count max min sum
select avg(prod_price) as avg_price from products
where vend_id=1003;#多个列平均 ,avg要使用多次 且自动忽略null
#count(*)对表中行计数
#count(columns)对这个列有数值的行计数
select count(*) as num_cust from customers;#返回所有客户数
select count(cust_email) as num_cust from customers;#返回有email的客户数
#max min sum(返回指定列值的和)
select max(prod_price) as max_price from products;
select min(prod_price) as max_price from products;
select * from orderitems;
select sum(quantity) as item_ordered from orderitems
where order_num=20005;
select sum(quantity*item_price) as total_price from orderitems
where order_num=20005;
##聚集不同的值
select avg(distinct prod_price) as avg_price from products
where vend_id=1003;
##组合聚集函数
select count(*) as num_items,
min(prod_price) as price_min,
avg(prod_price) as price_avg from products;
mysql 10-12
猜你喜欢
转载自blog.csdn.net/kylin_learn/article/details/80890515
今日推荐
周排行