1.利用计算字段的概念,对检索出的结果进行(字符的拼接,数字的计算)再展现出来,介绍了别名的概念
存储在数据库表中的数据一般都不是应用程序所需要的格式,有些时候需要把不同列的信息整合在一个列里,或者是对不同列进行数值运算,(这些操作虽然可以在客户端完成,但在服务器端完成更加快),这里就需要创建计算字段。
字段某种意义上就是列的意思,但一般也就只用在这里。
拼接字段:
将两个列的内容拼接后再输出。
比如vendors表包含vend_name和vend_country列,选择需要以name(country)的形式拼接再输出。
select Concat(vend_name,'(',vend_country,')') from vendors order by vend_name;
使用别名:
上面的语句虽然完成了拼接,但是输出后的列没有名字,使用关键字 as 可以解决
select Concat(vend_name,'(',vend_country,')') as vend_title from vendors order by vend_name;
这样输出的列就有名称vend_title
执行算术计算
select prod_id,quantity,item_prices,quantity*item_price as expanded_price from orderitem where num= 2005;
这里就把quantity和item_price 的结果相乘并且以 expanded_price 来输出列
计算 (+ - * /)
2.MySQL中的数据处理函数
sql中有许多特殊的处理函数,如文本处理函数,算数函数,时间函数,系统函数等
# 文本处理函数
select vend_name,Upper(vend_name) as upercase_name from vendors order by vend_name;
# Left() 返回串左边的字符
# Length() 返回串的长度
# Locate() 返回一个子串
# Upper() 将串转化为大写,Lower() 将串转化为小写
# LTrim() 去掉串左边的空格
# Soundex() 返回串的SOUNDEX值,SOUNDEX表示一个串的读音上的值,是一种算法
# SubString() 返回子串的字符
select cust_name,cust_contact from customers where Soundex(cust_name)=Soundex('Y Lie');
# 数值处理函数
查表
#日期处理函数
Date() 表示日期
其它可查表
3.MySQL中的聚集函数
处理的对象是一列的值,返回的是单个值
有些时候我们只需要对列中的信息进行汇总统计而无需真正的把他们检索出来,如 记录表中行数,行组的和,某个列的最大值/平均值
聚集函数:运行在行组上,计算和返回单个值的函数
select AVG(prod_preice) as avg_price from products where vend_id=1003;
# AVG() 返回单个列的平均值
select COUNT(prod_preice) as c_price from products where vend_id=1003;
# COUNT(col) 对单个列进行计数
# COUNT(*) 对单个行进行计数, 看有几行,这样可以表示行!!!!!!!!
select MAX(prod_preice) as max_price from products where vend_id=1003;
# MAX() 返回单个列的最大值,若使用的对象是非数值数据,则返回最后一行
select MIN(prod_preice) as min_price from products where vend_id=1003;
# MIN() 返回单个列的最小值,若使用的对象是非数值数据,则返回第一行
select SUM(prod_preice) as sum_price from products where vend_id=1003;
select SUM(prod_preice*quantity) as total_price from products;
# SUM() 返回单个列的和(总计),当然也可以对多个列的计算结果进行统计
select AVG(distinct prod_preice) as avg_price from products where vend_id=1003;
# 这样就只对不同的价格进行计算
组合聚合函数
select AVG(prod_preice) as avg_price, COUNT(prod_preice) as c_price, MAX(prod_preice) as max_price from product;
4.数据分组
这里的处理对象不是单个值,也不是全列,而是依据自己制定的规则在一列中分的组,以组为单位进行操作(group by)
select COUNT(*) as num_prods from products where vend_id=1003;
# 返回供应商id为1003提供的产品的数目(即多少行)。
但如果要返回每个供应商提供的产品数目,这种时候就需要分组了。
创建分组
select vend_id,COUNT(*) as num_prods from products group by vend_id;
# 按照vend_id来分组后检索vend_id并且分组计算相对应的行数
# 若分组中涉及NULL,则NULL将会作为一个单独的分组
# group by 子句必须出现在where子句之后,order by子句之前
过滤分组
过滤分组表示包括哪些分组,排除哪些分组,比如要列出至少有两个分组的内容
where子句能够起到过滤的作用,但是where不能作用于分组,只能作用于行
过滤分组用having来替代,并且having支持所有的where操作符
select vend_id,COUNT(*) as num_prods from products group by vend_id having COUNT(*)>=2;
# where是在分组前进行过滤,having是在分组后进行过滤
select vend_id,COUNT(*) as num_prods from products where prod_price>=10 group by vend_id HAVING COUNT(*)>=2;
# 先用where过滤,对过滤后的进行分组,对分组后的结果再用HAVING进行过滤后返回
group by 是分组,但是输出的结果没有顺序,往往需要用order by进行排序。
select vend_id,COUNT(*) as num_prods from products where prod_price>=10 group by vend_id HAVING COUNT(*)>=2 order by vend_id;
select语句的顺序
select-from-where-group by-having-order by-limit