MySQL 查询语句练习1
Posted LCQ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 查询语句练习1相关的知识,希望对你有一定的参考价值。
1、创建成绩表,字段包括:学生姓名,语文成绩,数学成绩,英语成绩
向表中插入多条数据;
查询:
(1) 查询所有学生的数学成绩和总成绩
(2) 查询所有学生的语文和数学成绩和,按从高到低排序
(3) 查询班级总成绩最高的学生姓名
(4) 查询班里所有姓李学生的总成绩最高的姓名
创建表
CREATE TABLE grade ( name VARCHAR(20) NOT NULL , chinese_score FLOAT NOT NULL , math_score FLOAT NOT NULL , english_score FLOAT NOT NULL ) CHARACTER SET utf8;
mysql> DESC grade; +---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | name | varchar(20) | NO | | NULL | | | chinese_score | float | NO | | NULL | | | math_score | float | NO | | NULL | | | english_score | float | NO | | NULL | | +---------------+-------------+------+-----+---------+-------+
插入记录
INSERT grade VALUES(‘李白‘,88,99,77),(‘杜甫‘,77,88,99),(‘白居易‘,99,88,77);
1、
mysql> SELECT SUM(math_score) FROM grade; +-----------------+ | SUM(math_score) | +-----------------+ | 275 | +-----------------+ 1 row in set (0.00 sec)
2、
mysql> SELECT name,chinese_score+math_score AS ‘语文+数学‘ FROM grade ORDER BY chinese_score+grade.math_score DESC ; +-----------+---------------+ | name | 语文+数学 | +-----------+---------------+ | 李白 | 187 | | 白居易 | 187 | | 杜甫 | 165 | +-----------+---------------+ 3 rows in set (0.00 sec)
3、
mysql> SELECT name AS ‘姓名‘,chinese+math+english AS ‘总分‘ FROM grade WHERE(chinese+math+english=(SELECT MAX(chinese+math+english)FROM grade)); +--------+--------+ | 姓名 | 总分 | +--------+--------+ | 李白 | 264 | +--------+--------+ 1 row in set (0.00 sec)
4、
mysql> SELECT name AS ‘姓名‘,chinese+math+english AS ‘总分‘ FROM grade WHERE(chinese+math+english=(SELECT MAX(chinese+math+english)FROM grade)and name like ‘李%‘); +--------+--------+ | 姓名 | 总分 | +--------+--------+ | 李白 | 264 | +--------+--------+ 1 row in set (0.00 sec)
另一种方法,高分排序,取第一个,order by ,limit 1
2、创建一张某超市的购物表,字段包括:商品名,购物价格,商品生茶日期,商品分类;
向该表中插入多条数据;
查询:(1)每一类商品花的总价格
(2)统计每类商品各有多少件
(3)统计水果花了多少钱(两种方式实现)
(4)统计购买的2017-01-12日生产的商品中价格最贵的商品(插入的数据中包括2017-01-12生产的商品)
(5)统一购买商品的总价格
CREATE TABLE order_menu( id INT PRIMARY KEY auto_increment, product_name VARCHAR (20), price FLOAT(6,2), born_date DATE, class VARCHAR (20) ) CHARACTER SET utf8; INSERT order_menu (product_name,price,born_date,class) VALUES ("苹果",20,20170112,"水果"), ("香蕉",80,20170602,"水果"), ("水壶",120,20170112,"电器"), ("被罩",70,20170612,"床上用品"), ("音响",420,20170112,"电器"), ("床单",55,20170612,"床上用品"), ("草莓",34,20170612,"水果"); mysql> DESC order_menu; +--------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | product_name | varchar(20) | YES | | NULL | | | price | float(6,2) | YES | | NULL | | | born_date | date | YES | | NULL | | | class | varchar(20) | YES | | NULL | | +--------------+-------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec)
1、
mysql> SELECT class,sum(price) FROM order_menu GROUP BY class; +--------------+------------+ | class | sum(price) | +--------------+------------+ | 床上用品 | 125.00 | | 水果 | 134.00 | | 电器 | 540.00 | +--------------+------------+ 3 rows in set (0.00 sec)
2、
mysql> SELECT class,count(product_name) FROM order_menu GROUP BY class; +--------------+---------------------+ | class | count(product_name) | +--------------+---------------------+ | 床上用品 | 2 | | 水果 | 3 | | 电器 | 2 | +--------------+---------------------+ 3 rows in set (0.00 sec)
3、
mysql> SELECT sum(price) FROM order_menu WHERE class=‘水果‘; +------------+ | sum(price) | +------------+ | 134.00 | +------------+ 1 row in set (0.00 sec)
3、
mysql> SELECT class,sum(price) FROM order_menu GROUP BY class HAVING class=‘水果‘; +--------+------------+ | class | sum(price) | +--------+------------+ | 水果 | 134.00 | +--------+------------+ 1 row in set (0.00 sec)
4、
mysql> SELECT product_name,price FROM order_menu WHERE born_date=20170112 ORDER BY price DESC limit 1; +--------------+--------+ | product_name | price | +--------------+--------+ | 音响 | 420.00 | +--------------+--------+ 1 row in set (0.00 sec)
5、
mysql> SELECT sum(price) FROM order_menu; +------------+ | sum(price) | +------------+ | 799.00 | +------------+ 1 row in set (0.00 sec)
以上是关于MySQL 查询语句练习1的主要内容,如果未能解决你的问题,请参考以下文章