10.创建计算字段
计算字段并不实际存在于数据库表中。计算字段是运行时在你select语句内创建的。
字段(filed):基本上与列的意思相同,经常互换使用,不过数据库列一般称为列,而术语字段通常用在计算字段的连接上。
10.1拼接字段
拼接:将值联结到一起构成单个值。
concat()函数:多数dbms使用+或||实现拼接,而mysql使用concat()函数实现。
rtrim()函数:去掉值右边的所有空格
mysql> select concat(rtrim(vend_name),' (',rtrim(vend_country),')')from vendors
order by vend_name;
+-------------------------------------------------------+
| concat(rtrim(vend_name),'(',rtrim(vend_country),')') |
+-------------------------------------------------------+
| acme (usa) |
| anvils r us (usa) |
| furball inc. (usa) |
| jet set (england) |
| jouets et ours (france) |
| lt supplies (usa) |
+-------------------------------------------------------+
6 rows in set (0.00 sec)
10.2使用别名
别名用as关键字赋予。
mysql> select concat(rtrim(vend_name),'(',rtrim(vend_country),')') as vend_titl
e from vendors order by vend_name;
+-------------------------+
| vend_title |
+-------------------------+
| acme (usa) |
| anvils r us (usa) |
| furball inc. (usa) |
| jet set (england) |
| jouets et ours (france) |
| lt supplies (usa) |
+-------------------------+
6 rows in set (0.00 sec)
输出结果与以前相同,但是现在列名为vend_title。
10.3执行算术计算
selectprod_id,quantity,item_price,quantity*item_price asexpanded_price fromorderitems where order_num = 20005;
+---------+----------+------------+------------------+
| prod_id | quantity | item_price |asexpanded_price |
+---------+----------+------------+------------------+
| anv01 | 10 | 5.99 | 59.90 |
| anv02 | 3 | 9.99 | 29.97 |
| tnt2 | 5 | 10.00 | 50.00 |
| fb | 1 | 10.00 | 10.00 |
+---------+----------+------------+------------------+
4 rows in set (0.03 sec)
11.使用数据处理函数
使用函数
大多数sql实现支持以下类型的函数:
1) 用于处理文本串(如删除或填充值,转换值大小写)的文本函数
2) 用于在数值数据上进行算术操作(如返回绝对值,进行代数运算的数值函数)
3) 用于出来日期和时间并从这些值中提取特定成分(如返回日期之差)的日期和时间函数
4) 返回dbms正使用的特殊信息(如返回用户登录信息)的系统函数
11.1文本处理函数
left():返回串左边的字符
length():返回串的长度
locate():找出串的一个字串
lower():将串转换为小写
ltrim():去掉左边的空格
right():返回串右边的字符
rtrim():去除列值右边的空格
soundex():返回串的soundex值,将任何文本串转换为描述其语音表示的字母数字模式算法
substring():返回子串的字符
upper():将文本转换为大写
select vend_name,upper(vend_name) asvend_name_upcase from vendors;
select cust_name,cust_contact from customerswhere soundex(cust_contact)= soundex('y lie');//结果如下,输出cust_contact发音和'y lie'一致的结果,这里发音一致的是y lee。
+-------------+--------------+
| cust_name | cust_contact |
+-------------+--------------+
| coyote inc. | y lee |
+-------------+--------------+
1 row in set (0.00 sec)
11.2日期和时间处理函数
adddate() 增加一个日期
addtime() 增加一个时间
curdate() 返回当前日期
data() 返回日期时间的日期部分
datadiff() 返回两个日期之差
data_add() 高度灵活的日期运算函数
data_format() 返回一个格式化的日期或时间串
day() 返回一个日期的天数部分
dayofweek () 对于一个日期,返回对应的星期几
hour() 返回一个时间的小时部分
minute() 返回一个时间的分钟部分
mounth() 返回一个日期的月份部分
now() 返回当前的日期和时间
second() 返回一个时间秒部分
time() 返回一个时间的实践部分
year() 返回一个日期的年份部分
mysql日期格式为yyyy-mm-dd
mysql> select cust_id,order_num fromorders where date(order_date) = '2005-09-01
';//输出结果如下
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
| 10001 | 20005 |
+---------+-----------+
1 row in set (0.01 sec)
11.3数值处理函数
abs() 返回一个数的绝对值
cos() 一个角度的余弦值
exp() 一个数的指数值
mod() 除操作数的余数
pi() 返回圆周率
rand() 返回一个随机数
sin() 一个角度的正弦
sqrt() 一个数的平方根
tan() 一个角度的正切
12.汇总数据
12.1聚集函数
聚集函数:运行在行组山,计算和返回单个值的函数
avg() 某列的平均值,忽略值为null的行
count() 某列的行数,忽略值为null的行
max() 某列的最大值
min() 某列的最小值
sum() 某列值之和
还支持一些标准偏差聚集函数,这里不涉及。
select avg(prod_price) as avg_price fromproducts;
+-----------+
| avg_price |
+-----------+
| 16.133571 |
+-----------+
1 row in set (0.03 sec)
12.聚集不同的值
select avg(distinct prod_price) as avg_pricefrom products where vend_id = 1003;
+-----------+
| avg_price |
+-----------+
| 15.998000 |
+-----------+
1 row in set (0.03 sec)
可以看到,在使用了distinct后,此例子中的avg_price比较高,因为有多个物品具有相同的较低价格。
12.3组合聚集函数:
select avg(prod_price) asavg_price,min(prod_price) as price_min from products where vend_id = 1003;
13.分组数据
分组允许把数据分为多个逻辑组,以便能对每个组进行聚集计算。
13.1创建分组
mysql> select vend_id,count(*) asnum_prods from products group by vend_id;
+---------+-----------+
| vend_id | num_prods |
+---------+-----------+
| 1001 | 3 |
| 1002 | 2 |
| 1003 | 7 |
| 1005 | 2 |
+---------+-----------+
4 rows in set (0.05 sec)
group by子句指示mysql分组数据,然后对每个组而不是整个结果进行聚集。
使用group by的重要规则:
1) 可以包含任意数目的列,可以嵌套分组
2) 除聚集语句外,select语句中的每个列都必须在groupby子句中给出
3) 如果分组中有null值,则将null作为一个分组
4) group by子句必须出现在where子句之后,order by子句之前
13.2过滤分组
having支持所有where操作符。
mysql> select cust_id,count(*) as ordersfrom orders group by cust_id having cou
nt(*) >= 2;
+---------+--------+
| cust_id | orders |
+---------+--------+
| 10001 | 2 |
+---------+--------+
1 row in set (0.00 sec)
这里的最后一行它过滤count(*)>=2(两个以上的订单)的那些分组。
having和where的区别:where在数据分组前进行过滤,having在数据分组后进行过滤。
13.3分组和排序
select order_num,sum(quantity*item_price) as ordertotal fromorderitems group by order_num having sum(quantity*item_price)>=50;
+-----------+------------+
| order_num | ordertotal |
+-----------+------------+
| 20005 | 149.87 |
| 20006 | 55.00 |
| 20007 | 1000.00 |
| 20008 | 125.00 |
+-----------+------------+
4 rows in set (0.00 sec)
13.4 select子句的顺序
select->from->where->group by->having->orderby->limit
14 使用子查询
14.1利用子查询进行过滤
select cust_name,cust_contact from customers where cust_id in(select cust_id from orders where order_num in (select order_num fromorderitems where prod
_id = 'tnt2'));
+----------------+--------------+
| cust_name |cust_contact |
+----------------+--------------+
| coyote inc. | y lee |
| yosemite place | y sam |
+----------------+--------------+
2 rows in set (0.11 sec)
虽然子查询一般与in操作符结合使用,但也可以用于测试等于(=)、不等于()等。
14.2作为计算字段使用子查询
select cust_name,cust_state,(select count(*) from orders whereorders.cust_id = customers.cust_id) as orders from customers order bycust_name;
+----------------+------------+--------+
| cust_name | cust_state| orders |
+----------------+------------+--------+
| coyote inc. | mi | 2 |
| e fudd | il | 1 |
| mouse house | oh | 0 |
| wascals | in | 1 |
| yosemite place | az | 1 |
+----------------+------------+--------+
5 rows in set (0.00 sec)
这条select语句对customers表中每个客户返回3列:cust_name,cust_state和orders.orders是一个计算字段,它是由圆括号中的子查询建立的。该子查询对检索出的每个客户执行一次。在此例子中,该子查询执行了5次,因为检索除了5个客户。
==参考mysql必知必会
bitscn.com
