MySQL第四课(表的增删查改)

一、增添
规则1:
  • 插入的数据应与字段的数据类型相同。比如,将‘abc'插入到id列就不行:
mysql> create database if not exists bit27_4 default charset=utf8;
Query OK, 1 row affected (0.00 sec)

mysql> use bit27_4;
Database changed
mysql> create table goods (id int primary key, goods_name varchar(64) not null default '', price float not null default 0.0);
Query OK, 0 rows affected (0.04 sec)

mysql> desc goods
    -> ;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | NO   | PRI | NULL    |       |
| goods_name | varchar(64) | NO   |     |         |       |
| price      | float       | NO   |     | 0       |       |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into goods values ('abc','牛排',32.0);    //插入时类型不对应
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from goods;
+----+------------+-------+
| id | goods_name | price |
+----+------------+-------+
|   0 | 牛排     |    32 |
+----+------------+-------+
1 row in set (0.00 sec)

规则2:
  • 数据的大小应在规定的范围内,例如:不能将一个长度为80的字符串插入到长度为40的列中。(会截断)
mysql> alter table goods modify goods_name varchar(6) not null default '';
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> desc goods;
+------------+------------+------+-----+---------+-------+
| Field      | Type       | Null | Key | Default | Extra |
+------------+------------+------+-----+---------+-------+
| id         | int(11)    | NO   | PRI | NULL    |       |
| goods_name | varchar(6) | NO   |     |         |       |
| price      | float      | NO   |     | 0       |       |
+------------+------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into goods values (1,'12345678',32.0);     //插入的数据长度超过限制
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from goods;
+----+------------+-------+
| id | goods_name | price |
+----+------------+-------+
|  0 | 牛排     |    32 |
|  1 | 123456     |    32 |
+----+------------+-------+
2 rows in set (0.00 sec)

规则3: 
  • 在values中列出的数据位置必须与被加入的列位置相对应。(数据类型要匹配)
mysql> insert into goods values (2,32.0,'牛排');
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from goods;
+----+------------+-------+
| id | goods_name | price |
+----+------------+-------+
|  0 | 牛排     |    32 |
|  1 | 123456     |    32 |
|  2 | 32.0       |      0 |
+----+------------+-------+
3 rows in set (0.00 sec)

规则4:
  • 字符和日期类型应该包含在单引号中。
mysql> set sql_mode=strict_trans_tables;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into goods values (3,西瓜,'4.5');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '??瓜,'4.5')' at line 1
mysql> insert into goods values (3,'西瓜','4.5');
Query OK, 1 row affected (0.00 sec)

规则5:
  • 插入空值,不指定或insert into table values(null)
  • insert into table values(),(),() 一次性添加多条记录
mysql> create table t1 (name varchar(32));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values (null);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+------+
| name |
+------+
| NULL |
+------+
1 row in set (0.00 sec)

mysql> insert  into  goods  values (1,'aa',3.3) , (2,'bb',4.4) , (3,'cc',5.5);                //一次性添加三条记录
Query OK, 3 rows affected (0.00 sec)

  • 规则6:
  • 如果只给表的某几个字段赋值,则需要制定字段名
  • 如果给表中的所有字段添加数据,可以不写前面的字段名称。
mysql> insert into goods(id,goods_name) values (4,'手机');
Query OK, 1 row affected (0.00 sec)

mysql> insert into goods values(4,'phone',2.5);      //如果没有给出字段名称,values中必须给出所有的字段值
Query OK, 1 row affected (0.00 sec)

规则7:
  • 在数据插入的时候,假设主键对应的值已经存在:插入失败!
  • 当主键存在冲突的时候(duplicate key),可以选择性的进行处理:更新操作、替换操作(主键如果没有冲突,就直接插入)。

更新
mysql>   alter table goods modify goods_name varchar(64) not null default '';
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> desc goods;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | NO   | PRI | NULL    |       |
| goods_name | varchar(64) | NO   |     |         |       |
| price      | float       | NO   |     | 0       |       |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into goods(id,goods_name) values (4,'手机2');
ERROR 1062 (23000): Duplicate entry '4' for key 'PRIMARY'        //插入失败
mysql> insert into goods(id,goods_name) values (4,'手机2') on duplicate key update goods_name='手机2';
Query OK, 2 rows affected (0.00 sec)      //更新操作,将id=4的数据的指定字段(goods_name)改为手机2

mysql> select * from goods;
+----+------------+-------+
| id | goods_name | price |
+----+------------+-------+
|  0 | 牛排     |    32 |
|  1 | 123456     |    32 |
|  2 | 32.0       |     0 |
|  3 | 西瓜     |   4.5 |
|  4 | 手机2    |     0 |
+----+------------+-------+
5 rows in set (0.00 sec)
mysql> insert into goods(id,goods_name,price) values (4,'手机2',4.6) on duplicate key update goods_name='手机2', price=4.6;
Query OK, 2 rows affected (0.00 sec)

mysql> select * from goods;
+----+------------+-------+
| id | goods_name | price |
+----+------------+-------+
|  0 | 牛排     |    32 |
|  1 | 123456     |    32 |
|  2 | 32.0       |     0 |
|  3 | 西瓜     |   4.5 |
|  4 | 手机2    |   4.6 |
+----+------------+-------+
5 rows in set (0.00 sec)
(替换操作:另一种插入数据的方式)
mysql> replace into goods values (100,'huawei',999);    //主键如果没有冲突,就直接插入
Query OK, 1 row affected (0.00 sec)

mysql> select * from goods;
+-----+------------+-------+
| id  | goods_name | price |
+-----+------------+-------+
|   0 | 牛排     |    32 |
|   1 | 123456     |    32 |
|   2 | 32.0       |     0 |
|   3 | 西瓜     |   4.5 |
|   4 | 手机2    |   4.6 |
| 100 | huawei     |   999 |
+-----+------------+-------+
6 rows in set (0.00 sec)

二、修改
1、update
update使用细节:
  • update 语法可以用新值更新原有表中的各列
  • set子句指示要修改哪些列和要给予哪些值
  • where子句指定应更新哪些行。如果没有where子句,则更新所有行
  • 如果需要更新多个字段,可以通过 set 字段1=值1,字段2=值2...
mysql> update goods set price = 300;    //将所有商品(整表)的价格修改为300
Query OK, 6 rows affected (0.00 sec)
Rows matched: 6  Changed: 6  Warnings: 0

mysql> select * from goods;
+-----+------------+-------+
| id  | goods_name | price |
+-----+------------+-------+
|   0 | 牛排     |   300 |
|   1 | 123456     |   300 |
|   2 | 32.0       |   300 |
|   3 | 西瓜     |   300 |
|   4 | 手机2    |   300 |
| 100 | huawei     |   300 |
+-----+------------+-------+
6 rows in set (0.00 sec)
mysql> update goods set price=1000 where id=100;    //id为100的商品价格修改为1000
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from goods;
+-----+------------+-------+
| id  | goods_name | price |
+-----+------------+-------+
|   0 | 牛排     |   300 |
|   1 | 123456     |   300 |
|   2 | 32.0       |   300 |
|   3 | 西瓜     |   300 |
|   4 | 手机2    |   300 |
| 100 | huawei     |  1000 |
+-----+------------+-------+
6 rows in set (0.00 sec)
  
mysql> update goods set price=price+200 where id=101;     //将id为101的产品价格增加200块
Query OK, 1 row affected (0.00 sec)
Rows matched:1 Changed:1 Warnings:0
(修改多列)
mysql> update goods set goods_name='xiaomi',price=998 where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from goods;
+-----+------------+-------+
| id  | goods_name | price |
+-----+------------+-------+
|   0 | 牛排     |   301 |
|   1 | 123456     |   301 |
|    2 | xiaomi     |   998 |
|   3 | 西瓜     |   301 |
|   4 | 手机2    |   301 |
| 100 | huawei     |  1000 |
+-----+------------+-------+
6 rows in set (0.00 sec)
(限制更新数量)
mysql> update goods set price = 300 limit 2;    //只更新两行(默认前两行)
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from goods;
+-----+------------+-------+
| id  | goods_name | price |
+-----+------------+-------+
|   0 | 牛排     |    300 |
|   1 | 123456     |    300 |
|   2 | xiaomi     |   998 |
|   3 | 西瓜     |   301 |
|   4 | 手机2    |   301 |
| 100 | huawei     |  1000 |
+-----+------------+-------+
6 rows in set (0.00 sec)

三、删除
1、delete
  • 如果不使用where子句,将删除整个表中所有数据
  • delete语句不能删除某一列的值(可以用update置null)
  • 使用delete语句仅删除记录,不删除表本身(drop table)
mysql> delete from goods where id=100;
Query OK, 1 row affected (0.00 sec)

mysql> select * from goods;
+----+------------+-------+
| id | goods_name | price |
+----+------------+-------+
|  0 | 牛排     |   300 |
|  1 | 123456     |   300 |
|  2 | xiaomi     |   998 |
|  3 | 西瓜     |   301 |
|  4 | 手机2    |   301 |
+----+------------+-------+
5 rows in set (0.00 sec)
(在练习删除时,可以复制一份表,避免数据删没了)
mysql> create table goods2 like goods;     //复制表结构
Query OK, 0 rows affected (0.01 sec)

mysql> desc goods2;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | NO   | PRI | NULL    |       |
| goods_name | varchar(64) | NO   |     |         |       |
| price      | float       | NO   |     | 0       |       |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into goods2 select * from goods;    //因为两个表的表结构(字段名、数据类型、表的约束)完全相同,所以可以直接移植过去。
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from goods2;
+----+------------+-------+
| id | goods_name | price |
+----+------------+-------+
|  0 | 牛排     |   300 |
|  1 | 123456     |   300 |
|  2 | xiaomi     |   998 |
|  3 | 西瓜     |   301 |
|  4 | 手机2    |   301 |
+----+------------+-------+
5 rows in set (0.00 sec)
2、truncate
mysql> delete from goods;     //删除表中所有数据,给出几条数据受影响
Query OK, 5 rows affected (0.00 sec)

mysql> select * from goods;
Empty set (0.00 sec)

mysql> truncate goods2;       //快速(所以清空时用这个)    不返回多少行数据受影响   都不会清除表结构
Query OK, 0 rows affected (0.00 sec)

mysql> select * from goods2;
Empty set (0.00 sec)
3、delete和truncate的区别:
  • 效果一样,truncate速度快;
  • delete可以带where条件,删除更加灵活;
  • delete可以返回被删除的记录数,而truncate返回0
  • 推荐使用delete进行删除,推荐使用truncate进行清空。

四、查询

mysql> source /home/du/student_data.sql       //把student_data.sql文件粘贴到linux系统的/home/du下,然后在虚拟机上运行该语句,可导入文件内容
Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Database changed
Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

+----+--------------+---------+---------+------+
| id | name         | chinese | english | math |
+----+--------------+---------+---------+------+
|  1 | 李涛       |    89.0 |    78.0 | 90.0 |
|  2 | 唐僧       |    67.0 |    98.0 | 56.0 |
|  3 | 孙悟空    |    87.0 |    78.0 | 77.0 |
|  4 | 老妖婆    |    88.0 |    98.0 | 90.0 |
|  5 | 红孩儿    |    82.0 |    84.0 | 67.0 |
|  6 | 如来佛祖 |    55.0 |    85.0 | 45.0 |
|  7 | 菩萨       |    75.0 |    65.0 | 30.0 |
+----+--------------+---------+---------+------+
7 rows in set (0.00 sec)
(指定列查询)
mysql> select id,name,math from student;
+----+--------------+------+
| id | name         | math |
+----+--------------+------+
|  1 | 李涛       | 90.0 |
|  2 | 唐僧       | 56.0 |
|  3 | 孙悟空    | 77.0 |
|  4 | 老妖婆    | 90.0 |
|  5 | 红孩儿    | 67.0 |
|  6 | 如来佛祖 | 45.0 |
|  7 | 菩萨       | 30.0 |
+----+--------------+------+
7 rows in set (0.00 sec)
(去除重复行查询)
mysql> select math from student;
+------+
| math |
+------+
| 90.0 |
| 56.0 |
| 77.0 |
| 90.0 |
| 67.0 |
| 45.0 |
| 30.0 |
+------+
7 rows in set (0.00 sec)

mysql> select distinct math from student;
+------+
| math |
+------+
| 90.0 |
| 56.0 |
| 77.0 |
| 67.0 |
| 45.0 |
| 30.0 |
+------+
6 rows in set (0.00 sec)
(列运算[类型为可运算类型])
mysql> select id,name,chinese+english+math from student;
+----+--------------+----------------------+
| id | name         | chinese+english+math |
+----+--------------+----------------------+
|  1 | 李涛       |                257.0 |
|  2 | 唐僧       |                221.0 |
|  3 | 孙悟空    |                242.0 |
|  4 | 老妖婆    |                276.0 |
|  5 | 红孩儿    |                233.0 |
|  6 | 如来佛祖 |                185.0 |
|  7 | 菩萨       |                170.0 |
+----+--------------+----------------------+
7 rows in set (0.00 sec)
(设置列别名)
mysql> select id,name,chinese+english+math as total_score from student;       //as可省略,中间用空格分开即可
+----+--------------+-------------+
| id | name         | total_score |
+----+--------------+-------------+
|  1 | 李涛       |       257.0 |
|  2 | 唐僧       |       221.0 |
|  3 | 孙悟空    |       242.0 |
|  4 | 老妖婆    |       276.0 |
|  5 | 红孩儿    |       233.0 |
|  6 | 如来佛祖 |       185.0 |
|  7 | 菩萨       |       170.0 |
+----+--------------+-------------+
7 rows in set (0.00 sec)
mysql> select id '编号' ,name '姓名', chinese+english+math '总分' from student;      //省略了as  
+--------+--------------+--------+
| 编号 | 姓名       | 总分 |
+--------+--------------+--------+
|      1 | 李涛       |  257.0 |
|      2 | 唐僧       |  221.0 |
|      3 | 孙悟空    |  242.0 |
|      4 | 老妖婆    |  276.0 |
|      5 | 红孩儿    |  233.0 |
|      6 | 如来佛祖 |  185.0 |
|      7 | 菩萨       |  170.0 |
+--------+--------------+--------+
7 rows in set (0.00 sec)
mysql> select id '编号' ,name '姓名', chinese+english+math+10 '总分' from student;
+--------+--------------+--------+
| 编号 | 姓名       | 总分 |
+--------+--------------+--------+
|      1 | 李涛       |  267.0 |
|      2 | 唐僧       |  231.0 |
|      3 | 孙悟空    |  252.0 |
|      4 | 老妖婆    |  286.0 |
|      5 | 红孩儿    |  243.0 |
|      6 | 如来佛祖 |  195.0 |
|      7 | 菩萨       |  180.0 |
+--------+--------------+--------+
7 rows in set (0.00 sec)

mysql> select id '编号' ,name '姓名', (chinese+english+math)*1.6 '总分' from student;
+--------+--------------+--------+
| 编号 | 姓名       | 总分 |
+--------+--------------+--------+
|      1 | 李涛       |  411.2 |
|      2 | 唐僧       |  353.6 |
|      3 | 孙悟空    |  387.2 |
|      4 | 老妖婆    |  441.6 |
|      5 | 红孩儿    |  372.8 |
|      6 | 如来佛祖 |  296.0 |
|      7 | 菩萨       |  272.0 |
+--------+--------------+--------+
7 rows in set (0.00 sec)

mysql> select id '编号' ,name '姓名', (chinese+english+math)*1.6 '总分' from student where name like '唐%';  //%表任意
+--------+--------+--------+
| 编号 | 姓名 | 总分 |
+--------+--------+--------+
|      2 | 唐僧 |  353.6 |
+--------+--------+--------+
1 row in set (0.00 sec)
(查询总分大于200分的同学的信息)
mysql> select id,name,(chinese+english+math) as total from student where total>200;
ERROR 1054 (42S22): Unknown column 'total' in 'where clause'
mysql> select id,name,(chinese+english+math) as total from student where (chinese+english+math)>200;    //where后不能用别名
+----+-----------+-------+
| id | name      | total |
+----+-----------+-------+
|  1 | 李涛    | 257.0 |
|  2 | 唐僧    | 221.0 |
|  3 | 孙悟空 | 242.0 |
|  4 | 老妖婆 | 276.0 |
|  5 | 红孩儿 | 233.0 |
+----+-----------+-------+
5 rows in set (0.00 sec)
(一些练习)
mysql> select * from student;
+----+--------------+---------+---------+------+
| id | name         | chinese | english | math |
+----+--------------+---------+---------+------+
|  1 | 李涛       |    89.0 |    78.0 | 90.0 |
|  2 | 唐僧       |    67.0 |    98.0 | 56.0 |
|  3 | 孙悟空    |    87.0 |    78.0 | 77.0 |
|  4 | 老妖婆    |    88.0 |    98.0 | 90.0 |
|  5 | 红孩儿    |    82.0 |    84.0 | 67.0 |
|  6 | 如来佛祖 |    55.0 |    85.0 | 45.0 |
|  7 | 菩萨       |    75.0 |    65.0 | 30.0 |
+----+--------------+---------+---------+------+
7 rows in set (0.00 sec)

mysql> select * from student where id>10 and name like '李%';
Empty set (0.00 sec)

mysql> select * from student where id>10 or name like '李%';
+----+--------+---------+---------+------+
| id | name   | chinese | english | math |
+----+--------+---------+---------+------+
|  1 | 李涛 |    89.0 |    78.0 | 90.0 |
+----+--------+---------+---------+------+
1 row in set (0.00 sec)
mysql> select id,name,(chinese+english+math) as total from student where (chinese+english+math)>200 and math<chinese and name like '唐%';
+----+--------+-------+
| id | name   | total |
+----+--------+-------+
|  2 | 唐僧 | 221.0 |
+----+--------+-------+
1 row in set (0.00 sec)

mysql> select id,name,english from student where english>=80 and english<=90;
+----+--------------+---------+
| id | name         | english |
+----+--------------+---------+
|  5 | 红孩儿    |    84.0 |
|  6 | 如来佛祖 |    85.0 |
+----+--------------+---------+
2 rows in set (0.00 sec)

mysql> select id,name,english from student where english between 80 and 90;     //between and两边都是闭区间
+----+--------------+---------+
| id | name         | english |
+----+--------------+---------+
|  5 | 红孩儿    |    84.0 |
|  6 | 如来佛祖 |    85.0 |
+----+--------------+---------+
2 rows in set (0.00 sec)
mysql> select id,name,math from student where math=89 or math=90 or math=91;
+----+-----------+------+
| id | name      | math |
+----+-----------+------+
|  1 | 李涛    | 90.0 |
|  4 | 老妖婆 | 90.0 |
+----+-----------+------+
2 rows in set (0.00 sec)

mysql> select id,name,math from student where math in(89,90,91);    //与上面的语句效果一样,更方便
+----+-----------+------+
| id | name      | math |
+----+-----------+------+
|  1 | 李涛    | 90.0 |
|  4 | 老妖婆 | 90.0 |
+----+-----------+------+
2 rows in set (0.00 sec)
(排序[默认升序]   order by 语句一般放在select的尾部)
mysql> select id,name,math from student order by math asc;     //asc即升序,默认也是升序
+----+--------------+------+
| id | name         | math |
+----+--------------+------+
|  7 | 菩萨       | 30.0 |
|  6 | 如来佛祖 | 45.0 |
|  2 | 唐僧       | 56.0 |
|  5 | 红孩儿    | 67.0 |
|  3 | 孙悟空    | 77.0 |
|  1 | 李涛       | 90.0 |
|  4 | 老妖婆    | 90.0 |
+----+--------------+------+
7 rows in set (0.00 sec)
mysql> select id,name,(chinese+english+math) as total from student order by (chinese+english+math) desc;    //desc降序
+----+--------------+-------+
| id | name         | total |
+----+--------------+-------+
|  4 | 老妖婆    | 276.0 |
|  1 | 李涛       | 257.0 |
|  3 | 孙悟空    | 242.0 |
|  5 | 红孩儿    | 233.0 |
|  2 | 唐僧       | 221.0 |
|  6 | 如来佛祖 | 185.0 |
|  7 | 菩萨       | 170.0 |
+----+--------------+-------+
7 rows in set (0.00 sec)
mysql> select id,name,(chinese+english+math) as total from student order by total desc;  //查询的时候可以用total,where后面不可以
+----+--------------+-------+
| id | name         | total |
+----+--------------+-------+
|  4 | 老妖婆    | 276.0 |
|  1 | 李涛       | 257.0 |
|  3 | 孙悟空    | 242.0 |
|  5 | 红孩儿    | 233.0 |
|  2 | 唐僧       | 221.0 |
|  6 | 如来佛祖 | 185.0 |
|  7 | 菩萨       | 170.0 |
+----+--------------+-------+
7 rows in set (0.00 sec)

mysql> select id,name,(chinese+english+math) as total from student order by total,name desc;  
//total采用了默认的升序,先排总分,总分相同时再排名字????
+----+--------------+-------+
| id | name         | total |
+----+--------------+-------+
|  7 | 菩萨       | 170.0 |
|  6 | 如来佛祖 | 185.0 |
|  2 | 唐僧       | 221.0 |
|  5 | 红孩儿    | 233.0 |
|  3 | 孙悟空    | 242.0 |
|  1 | 李涛       | 257.0 |
|  4 | 老妖婆    | 276.0 |
+----+--------------+-------+
7 rows in set (0.00 sec)
(返回某一列的行总数)
mysql> select count(*) from student;      //返回整表的行数
+----------+
| count(*) |
+----------+
|        8 |
+----------+
1 row in set (0.00 sec)

mysql> select count(id) from student;
+-----------+
| count(id) |
+-----------+
|         8 |
+-----------+
1 row in set (0.00 sec)

mysql> select count(name) from student;
+-------------+
| count(name) |
+-------------+
|           8 |
+-------------+
1 row in set (0.00 sec)

mysql> select * from student;
+----+--------------+---------+---------+------+
| id | name         | chinese | english | math |
+----+--------------+---------+---------+------+
|  1 | 李涛       |    89.0 |    78.0 | 90.0 |
|  2 | 唐僧       |    67.0 |    98.0 | 56.0 |
|  3 | 孙悟空    |    87.0 |    78.0 | 77.0 |
|  4 | 老妖婆    |    88.0 |    98.0 | 90.0 |
|  5 | 红孩儿    |    82.0 |    84.0 | 67.0 |
|  6 | 如来佛祖 |    55.0 |    85.0 | 45.0 |
|  7 | 菩萨       |    75.0 |    65.0 | 30.0 |
|  8 | 李雅       |    79.0 |    31.0 | 56.0 |
+----+--------------+---------+---------+------+
8 rows in set (0.00 sec)
mysql> select count(math) from student where math>90;
+-------------+
| count(math) |
+-------------+
|           0 |
+-------------+
1 row in set (0.00 sec)
注意:【count(列名)会排除为null的情况】

(求和)
mysql> select sum(math) from student;
+-----------+
| sum(math) |
+-----------+
|     511.0 |
+-----------+
1 row in set (0.00 sec)

mysql> select sum(math) as m_total , sum(chinese) as c_total , sum(english) as e_total from student;
+---------+---------+---------+
| m_total | c_total | e_total |
+---------+---------+---------+
|   511.0 |   622.0 |   617.0 |
+---------+---------+---------+
1 row in set (0.00 sec)
(求平均值)
mysql> select sum(chinese) / count(chinese) from student;
+-------------------------------+
| sum(chinese) / count(chinese) |
+-------------------------------+
|                      77.75000 |
+-------------------------------+
1 row in set (0.00 sec)

mysql> select avg(chinese) from student;
+--------------+
| avg(chinese) |
+--------------+
|     77.75000 |
+--------------+
1 row in set (0.00 sec)
(求最大/最小值)
mysql> select id,name,max(math) from student;    //错误,不可带其他列,max和min都只能算自己这一列的。这里之后再求min时出错  
+----+--------+-----------+
| id | name   | max(math) |
+----+--------+-----------+
|  1 | 李涛 |      90.0 |
+----+--------+-----------+
1 row in set (0.00 sec)

mysql> select max(math) from student;
+-----------+
| max(math) |
+-----------+
|      90.0 |
+-----------+
1 row in set (0.00 sec)

mysql> select min(math) from student;
+-----------+
| min(math) |
+-----------+
|      30.0 |
+-----------+
1 row in set (0.00 sec)

分组语句
先利用  mysql> source /home/du/scott_data.sql   导入表
mysql> desc dept;
+--------+--------------------------+------+-----+---------+-------+
| Field  | Type                     | Null | Key | Default | Extra |
+--------+--------------------------+------+-----+---------+-------+
| deptno | int(2) unsigned zerofill | NO   |     | NULL    |       |
| dname  | varchar(14)              | YES  |     | NULL    |       |
| loc    | varchar(13)              | YES  |     | NULL    |       |
+--------+--------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> desc emp;
+----------+--------------------------+------+-----+---------+-------+
| Field    | Type                     | Null | Key | Default | Extra |
+----------+--------------------------+------+-----+---------+-------+
| empno    | int(6) unsigned zerofill | NO   |     | NULL    |       |
| ename    | varchar(10)              | YES  |     | NULL    |       |
| job      | varchar(9)               | YES  |     | NULL    |       |
| mgr      | int(4) unsigned zerofill | YES  |     | NULL    |       |
| hiredate | datetime                 | YES  |     | NULL    |       |
| sal      | decimal(7,2)             | YES  |     | NULL    |       |
| comm     | decimal(7,2)             | YES  |     | NULL    |       |
| deptno   | int(2) unsigned zerofill | YES  |     | NULL    |       |
+----------+--------------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)

mysql> desc salgrade;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| grade | int(11) | YES  |     | NULL    |       |
| losal | int(11) | YES  |     | NULL    |       |
| hisal | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select deptno,avg(sal),max(sal) from emp group by deptno;    //以部门编号进行分组查询,即查询不同部门的部门编号、平均工资、最高工资
+--------+-------------+----------+
| deptno | avg(sal)    | max(sal) |
+--------+-------------+----------+
|     10 | 2916.666667 |  5000.00 |
|     20 | 2175.000000 |  3000.00 |
|     30 | 1566.666667 |  2850.00 |
+--------+-------------+----------+
3 rows in set (0.00 sec)

mysql> select deptno,job,avg(sal),max(sal) from emp group by deptno,job;   //每个部门不同职位的平均工资、最大工资
+--------+-----------+-------------+----------+
| deptno | job       | avg(sal)    | max(sal) |
+--------+-----------+-------------+----------+
|     10 | CLERK     | 1300.000000 |  1300.00 |
|     10 | MANAGER   | 2450.000000 |  2450.00 |
|     10 | PRESIDENT | 5000.000000 |  5000.00 |
|     20 | ANALYST   | 3000.000000 |  3000.00 |
|     20 | CLERK     |  950.000000 |  1100.00 |
|     20 | MANAGER   | 2975.000000 |  2975.00 |
|     30 | CLERK     |  950.000000 |   950.00 |
|     30 | MANAGER   | 2850.000000 |  2850.00 |
|     30 | SALESMAN  | 1400.000000 |  1600.00 |
+--------+-----------+-------------+----------+

mysql> select deptno,avg(sal) from emp group by deptno;
+--------+-------------+
| deptno | avg(sal)    |
+--------+-------------+
|     10 | 2916.666667 |
|     20 | 2175.000000 |
|     30 | 1566.666667 |
+--------+-------------+
3 rows in set (0.00 sec)

mysql> select deptno,avg(sal) from emp group by deptno having avg(sal)<2000;  //这里不用where,用having
+--------+-------------+
| deptno | avg(sal)    |
+--------+-------------+
|     30 | 1566.666667 |
+--------+-------------+
1 row in set (0.00 sec)













猜你喜欢

转载自blog.csdn.net/gunqu_d/article/details/80681207