1、创建成绩表,字段包括:学生姓名,语文成绩,数学成绩,英语成绩
向表中插入多条数据;
查询:
(1) 查询所有学生的数学成绩和总成绩
mysql> select sum(shuxue) from aa;
+-------------+
| sum(shuxue) |
+-------------+
| 478 |
+-------------+
1 row in set (0.02 sec)
(2) 查询所有学生的语文和数学成绩和,按从高到低排序
mysql> select name,sum(yuwen+shuxue) from aa group by id order by sum(yuwen+shuxue) desc;
+-----------+-------------------+
| name | sum(yuwen+shuxue) |
+-----------+-------------------+
| 李杰 | 196 |
| 李姐 | 194 |
| 张海燕 | 189 |
| 方合意 | 189 |
| 雷光东 | 183 |
+-----------+-------------------+
5 rows in set (0.02 sec)
(2) 查询班级总成绩最高的学生姓名
mmysql> select name,max(yuwen+shuxue+yingyu) as a from aa group by name order by a desc limit 1;
+--------+------+
| name | a |
+--------+------+
| 李杰 | 294 |
+--------+------+
1 row in set (0.00 sec)
(3) 查询班里所有姓李学生的总成绩最高的姓名
mysql> select name,max(yuwen+shuxue+yingyu) as a from aa group by name regexp '^李' order by a desc limit 1;
+--------+------+
| name | a |
+--------+------+
| 李姐 | 294 |
+--------+------+
1 row in set (0.00 sec)
2、创建一张某超市的购物表,字段包括:商品名,购物价格,商品生茶日期,商品分类;
向该表中插入多条数据;
查询:(1)每一类商品花的总价格
mysql> select 种类,sum(价格) from gou group by 种类;
+--------+-------------+
| 种类 | sum(价格) |
+--------+-------------+
| 家用 | 65.00 |
| 水果 | 132.77 |
| 电器 | 66.48 |
+--------+-------------+
3 rows in set (0.03 sec)
(2)统计每类商品各有多少件
mysql> select 种类,count(价格) from gou group by 种类;
+--------+---------------+
| 种类 | count(价格) |
+--------+---------------+
| 家用 | 1 |
| 水果 | 2 |
| 电器 | 2 |
+--------+---------------+
3 rows in set (0.00 sec)
(2)统计水果花了多少钱(两种方式实现)
mysql> select 种类,sum(价格) from gou where 种类='水果';
+--------+-------------+
| 种类 | sum(价格) |
+--------+-------------+
| 水果 | 132.77 |
+--------+-------------+
1 row in set (0.00 sec)
mysql> select 种类,sum(价格) from gou group by 种类 having 种类='水果';
+--------+-------------+
| 种类 | sum(价格) |
+--------+-------------+
| 水果 | 132.77 |
+--------+-------------+
1 row in set (0.03 sec)
(3)统计购买的2017-07-25日生产的商品中价格最贵的商品(插入的数据中包括2017-07-25生产的商品)
mysql> select 名称,max(价格) from gou group by 名称 order by 价格 desc limit 1;
+--------+-------------+
| 名称 | max(价格) |
+--------+-------------+
| 梨子 | 76.45 |
+--------+-------------+
1 row in set (0.00 sec)
(4)统一购买商品的总价格
mysql> select sum(价格) from gou ;
+-------------+
| sum(价格) |
+-------------+
| 264.25 |
+-------------+
1 row in set (0.00 sec)