mysql 10-12

#######################
#第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;







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值