1. 程式人生 > >MySQL 查詢語句練習1

MySQL 查詢語句練習1

所有 -s 音響 round row clas 分類 lin nes

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