Mysql查询语句
创建employee表
mysql> create table employee(id int primary key,
-> name varchar(200),
-> dept varchar(200),
-> salary int,
-> edlevel int,
-> hiredate date);
Query OK, 0 rows affected (0.72 sec)
mysql> select * from employee;
Empty set (0.06 sec)
录入数据
mysql> insert into employee values(1,'张三','开发部',2000,3,now());
Query OK, 1 row affected, 1 warning (0.14 sec)
mysql> insert into employee values(2,'李四','开发部',2500,3,now());
Query OK, 1 row affected, 1 warning (0.03 sec)
mysql> insert into employee values(3,'王五','设计部',2600,5,now());
Query OK, 1 row affected, 1 warning (0.03 sec)
mysql> insert into employee values(4,'王柳','设计部',2300,4,now());
Query OK, 1 row affected, 1 warning (0.52 sec)
mysql> insert into employee values(5,'马青','设计部',2100,5,now());
Query OK, 1 row affected, 1 warning (0.03 sec)
mysql> insert into employee values(6,'找吧','销售部',3100,7,now());
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> insert into employee values(7,'留给','销售部',3500,7,now());
Query OK, 1 row affected, 1 warning (0.03 sec)
查询表格
mysql> select * from employee;
+----+--------+-----------+--------+---------+------------+
| id | name | dept | salary | edlevel | hiredate |
+----+--------+-----------+--------+---------+------------+
| 1 | 张三 | 开发部 | 2000 | 3 | 2013-09-16 |
| 2 | 李四 | 开发部 | 2500 | 3 | 2013-09-16 |
| 3 | 王五 | 设计部 | 2600 | 5 | 2013-09-16 |
| 4 | 王柳 | 设计部 | 2300 | 4 | 2013-09-16 |
| 5 | 马青 | 设计部 | 2100 | 5 | 2013-09-16 |
| 6 | 找吧 | 销售部 | 3100 | 7 | 2013-09-16 |
| 7 | 留给 | 销售部 | 3500 | 7 | 2013-09-16 |
+----+--------+-----------+--------+---------+------------+
7 rows in set (0.00 sec)
列出每个部门最高薪水
mysql> select max(salary) from employee group by(dept);
+-------------+
| max(salary) |
+-------------+
| 2500 |
| 2600 |
| 3500 |
+-------------+
3 rows in set (0.16 sec)
查询每个部门总薪水,平均薪水
mysql> select dept,max(salary),sum(salary),avg(salary) from employee group by(de
pt);
+-----------+-------------+-------------+-------------+
| dept | max(salary) | sum(salary) | avg(salary) |
+-----------+-------------+-------------+-------------+
| 开发部 | 2500 | 4500 | 2250.0000 |
| 设计部 | 2600 | 7000 | 2333.3333 |
| 销售部 | 3500 | 6600 | 3300.0000 |
+-----------+-------------+-------------+-------------+
3 rows in set (0.09 sec)
统计每个部门有多少人
mysql> select dept,max(salary),sum(salary),avg(salary),count(*) from employee gr
oup by(dept);
+-----------+-------------+-------------+-------------+----------+
| dept | max(salary) | sum(salary) | avg(salary) | count(*) |
+-----------+-------------+-------------+-------------+----------+
| 开发部 | 2500 | 4500 | 2250.0000 | 2 |
| 设计部 | 2600 | 7000 | 2333.3333 | 3 |
| 销售部 | 3500 | 6600 | 3300.0000 | 2 |
+-----------+-------------+-------------+-------------+----------+
3 rows in set (0.05 sec)
mysql>
统计最高薪水 平均薪水 部门人数(别名英文可以不加反引号,汉字加反引号,但规范不管英中文都加)
mysql> select dept `部门`,max(salary) `最高薪水`,sum(salary) `总薪水`,avg(salary
) `平均薪水`,count(*) `部门人数` from employee group by(dept);
+-----------+--------------+-----------+--------------+--------------+
| 部门 | 最高薪水 | 总薪水 | 平均薪水 | 部门人数 |
+-----------+--------------+-----------+--------------+--------------+
| 开发部 | 2500 | 4500 | 2250.0000 | 2 |
| 设计部 | 2600 | 7000 | 2333.3333 | 3 |
| 销售部 | 3500 | 6600 | 3300.0000 | 2 |
+-----------+--------------+-----------+--------------+--------------+
3 rows in set (0.03 sec)
mysql>
按照工作升序排列(默认升序)
mysql> select * from employee order by salary asc;
+----+--------+-----------+--------+---------+------------+
| id | name | dept | salary | edlevel | hiredate |
+----+--------+-----------+--------+---------+------------+
| 1 | 张三 | 开发部 | 2000 | 3 | 2013-09-16 |
| 5 | 马青 | 设计部 | 2100 | 5 | 2013-09-16 |
| 4 | 王柳 | 设计部 | 2300 | 4 | 2013-09-16 |
| 2 | 李四 | 开发部 | 2500 | 3 | 2013-09-16 |
| 3 | 王五 | 设计部 | 2600 | 5 | 2013-09-16 |
| 6 | 找吧 | 销售部 | 3100 | 7 | 2013-09-16 |
| 7 | 留给 | 销售部 | 3500 | 7 | 2013-09-16 |
+----+--------+-----------+--------+---------+------------+
7 rows in set (0.00 sec)
mysql> select * from employee order by salary;
+----+--------+-----------+--------+---------+------------+
| id | name | dept | salary | edlevel | hiredate |
+----+--------+-----------+--------+---------+------------+
| 1 | 张三 | 开发部 | 2000 | 3 | 2013-09-16 |
| 5 | 马青 | 设计部 | 2100 | 5 | 2013-09-16 |
| 4 | 王柳 | 设计部 | 2300 | 4 | 2013-09-16 |
| 2 | 李四 | 开发部 | 2500 | 3 | 2013-09-16 |
| 3 | 王五 | 设计部 | 2600 | 5 | 2013-09-16 |
| 6 | 找吧 | 销售部 | 3100 | 7 | 2013-09-16 |
| 7 | 留给 | 销售部 | 3500 | 7 | 2013-09-16 |
+----+--------+-----------+--------+---------+------------+
7 rows in set (0.00 sec)
按照工作降序排列
mysql> select * from employee order by salary desc;
+----+--------+-----------+--------+---------+------------+
| id | name | dept | salary | edlevel | hiredate |
+----+--------+-----------+--------+---------+------------+
| 7 | 留给 | 销售部 | 3500 | 7 | 2013-09-16 |
| 6 | 找吧 | 销售部 | 3100 | 7 | 2013-09-16 |
| 3 | 王五 | 设计部 | 2600 | 5 | 2013-09-16 |
| 2 | 李四 | 开发部 | 2500 | 3 | 2013-09-16 |
| 4 | 王柳 | 设计部 | 2300 | 4 | 2013-09-16 |
| 5 | 马青 | 设计部 | 2100 | 5 | 2013-09-16 |
| 1 | 张三 | 开发部 | 2000 | 3 | 2013-09-16 |
+----+--------+-----------+--------+---------+------------+
7 rows in set (0.00 sec)
部门最高工资大于3000的部门的最高工资和最低工资
mysql> select dept,max(salary) `maxsalary`,min(salary) `minsalary`from employee
group by dept having minsalary >3000;
+-----------+-----------+-----------+
| dept | maxsalary | minsalary |
+-----------+-----------+-----------+
| 销售部 | 3500 | 3100 |
+-----------+-----------+-----------+
1 row in set (0.03 sec)
平均工资大于2000的部门的最高、最低工资
mysql> select dept,max(salary) `maxsalary`,min(salary) `minsalary`,avg(salary) `
avgsalary`from employee group by dept having avgsalary > 2000;
+-----------+-----------+-----------+-----------+
| dept | maxsalary | minsalary | avgsalary |
+-----------+-----------+-----------+-----------+
| 开发部 | 2500 | 2000 | 2250.0000 |
| 设计部 | 2600 | 2100 | 2333.3333 |
| 销售部 | 3500 | 3100 | 3300.0000 |
+-----------+-----------+-----------+-----------+
平均工资大于2000的部门的最高、最低工资,并取前两位
mysql> select dept,max(salary)`maxsalary`,min(salary)`minsalary`,avg(salary)`avg
salary` from employee group by dept having avgsalary>2000 limit 2;
+-----------+-----------+-----------+-----------+
| dept | maxsalary | minsalary | avgsalary |
+-----------+-----------+-----------+-----------+
| 开发部 | 2500 | 2000 | 2250.0000 |
| 设计部 | 2600 | 2100 | 2333.3333 |
+-----------+-----------+-----------+-----------+
2 rows in set (0.05 sec)
平均工资大于2000的部门的最高、最低工资,并按照平均工资高低排序
并取前两位。
mysql> select dept,max(salary)`maxsalary`,min(salary)`minsalary`,avg(salary)`avg
salary` from employee group by dept having avgsalary>2000 order by avgsalary des
c limit 2;
+-----------+-----------+-----------+-----------+
| dept | maxsalary | minsalary | avgsalary |
+-----------+-----------+-----------+-----------+
| 销售部 | 3500 | 3100 | 3300.0000 |
| 设计部 | 2600 | 2100 | 2333.3333 |
+-----------+-----------+-----------+-----------+
2 rows in set (0.05 sec)
录入新的为空的信息
mysql> insert into employee(id,name,dept,salary)values(8,null,'开发部',null);
Query OK, 1 row affected (0.03 sec)
mysql> insert into employee(id,name,dept,salary)values(9,null,'开发部',null);
Query OK, 1 row affected (0.06 sec)
mysql> select * from employee;
+----+--------+-----------+--------+---------+------------+
| id | name | dept | salary | edlevel | hiredate |
+----+--------+-----------+--------+---------+------------+
| 1 | 张三 | 开发部 | 2000 | 3 | 2013-09-16 |
| 2 | 李四 | 开发部 | 2500 | 3 | 2013-09-16 |
| 3 | 王五 | 设计部 | 2600 | 5 | 2013-09-16 |
| 4 | 王柳 | 设计部 | 2300 | 4 | 2013-09-16 |
| 5 | 马青 | 设计部 | 2100 | 5 | 2013-09-16 |
| 6 | 找吧 | 销售部 | 3100 | 7 | 2013-09-16 |
| 7 | 留给 | 销售部 | 3500 | 7 | 2013-09-16 |
| 8 | NULL | 开发部 | NULL | NULL | NULL |
| 9 | NULL | 开发部 | NULL | NULL | NULL |
| 10 | NULL | NULL | NULL | NULL | NULL |
| 11 | NULL | NULL | NULL | NULL | NULL |
+----+--------+-----------+--------+---------+------------+
11 rows in set (0.00 sec)
查询信息为空的数据
mysql> select * from employee where name is null;
+----+------+-----------+--------+---------+----------+
| id | name | dept | salary | edlevel | hiredate |
+----+------+-----------+--------+---------+----------+
| 8 | NULL | 开发部 | NULL | NULL | NULL |
| 9 | NULL | 开发部 | NULL | NULL | NULL |
| 10 | NULL | NULL | NULL | NULL | NULL |
| 11 | NULL | NULL | NULL | NULL | NULL |
+----+------+-----------+--------+---------+----------+
4 rows in set (0.05 sec)
查询信息不为空的数据
mysql> select * from employee where name is not null;
+----+--------+-----------+--------+---------+------------+
| id | name | dept | salary | edlevel | hiredate |
+----+--------+-----------+--------+---------+------------+
| 1 | 张三 | 开发部 | 2000 | 3 | 2013-09-16 |
| 2 | 李四 | 开发部 | 2500 | 3 | 2013-09-16 |
| 3 | 王五 | 设计部 | 2600 | 5 | 2013-09-16 |
| 4 | 王柳 | 设计部 | 2300 | 4 | 2013-09-16 |
| 5 | 马青 | 设计部 | 2100 | 5 | 2013-09-16 |
| 6 | 找吧 | 销售部 | 3100 | 7 | 2013-09-16 |
| 7 | 留给 | 销售部 | 3500 | 7 | 2013-09-16 |
+----+--------+-----------+--------+---------+------------+
7 rows in set (0.03 sec)
Between and(>=and<=)的用法
mysql> select * from employee where salary between 2100 and 3100;
+----+--------+-----------+--------+---------+------------+
| id | name | dept | salary | edlevel | hiredate |
+----+--------+-----------+--------+---------+------------+
| 2 | 李四 | 开发部 | 2500 | 3 | 2013-09-16 |
| 3 | 王五 | 设计部 | 2600 | 5 | 2013-09-16 |
| 4 | 王柳 | 设计部 | 2300 | 4 | 2013-09-16 |
| 5 | 马青 | 设计部 | 2100 | 5 | 2013-09-16 |
| 6 | 找吧 | 销售部 | 3100 | 7 | 2013-09-16 |
+----+--------+-----------+--------+---------+------------+
5 rows in set (0.00 sec)
mysql> select * from employee where salary>=2100 and salary<=3100;
+----+--------+-----------+--------+---------+------------+
| id | name | dept | salary | edlevel | hiredate |
+----+--------+-----------+--------+---------+------------+
| 2 | 李四 | 开发部 | 2500 | 3 | 2013-09-16 |
| 3 | 王五 | 设计部 | 2600 | 5 | 2013-09-16 |
| 4 | 王柳 | 设计部 | 2300 | 4 | 2013-09-16 |
| 5 | 马青 | 设计部 | 2100 | 5 | 2013-09-16 |
| 6 | 找吧 | 销售部 | 3100 | 7 | 2013-09-16 |
+----+--------+-----------+--------+---------+------------+
通配符查询(模糊查询)
mysql> select * from employee where name like'王%';
+----+--------+-----------+--------+---------+------------+
| id | name | dept | salary | edlevel | hiredate |
+----+--------+-----------+--------+---------+------------+
| 3 | 王五 | 设计部 | 2600 | 5 | 2013-09-16 |
| 4 | 王柳 | 设计部 | 2300 | 4 | 2013-09-16 |
+----+--------+-----------+--------+---------+------------+
mysql> select * from employee where dept like'开__';
+----+--------+-----------+--------+---------+------------+
| id | name | dept | salary | edlevel | hiredate |
+----+--------+-----------+--------+---------+------------+
| 1 | 张三 | 开发部 | 2000 | 3 | 2013-09-16 |
| 2 | 李四 | 开发部 | 2500 | 3 | 2013-09-16 |
| 8 | NULL | 开发部 | NULL | NULL | NULL |
| 9 | NULL | 开发部 | NULL | NULL | NULL |
+----+--------+-----------+--------+---------+------------+
4 rows in set (0.00 sec)
In的用法
mysql> select * from employee where id in(2,3,4,5);
+----+--------+-----------+--------+---------+------------+
| id | name | dept | salary | edlevel | hiredate |
+----+--------+-----------+--------+---------+------------+
| 2 | 李四 | 开发部 | 2500 | 3 | 2013-09-16 |
| 3 | 王五 | 设计部 | 2600 | 5 | 2013-09-16 |
| 4 | 王柳 | 设计部 | 2300 | 4 | 2013-09-16 |
| 5 | 马青 | 设计部 | 2100 | 5 | 2013-09-16 |
+----+--------+-----------+--------+---------+------------+
4 rows in set (0.16 sec)
mysql>
关系运算符
!= 和 < > 表示不等于
mysql> select 1!=5;
+------+
| 1!=5 |
+------+
| 1 |
+------+
1 row in set (0.05 sec)
mysql> select 1<>2;
+------+
| 1<>2 |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql>
null=null和null<=>null;
(“=”表示关系运算符中的“赋值”,
“<=>”表示关系运算符中的“等于”)
mysql> select null=null,null<=>null;
+-----------+-------------+
| null=null | null<=>null |
+-----------+-------------+
| NULL | 1 |
+-----------+-------------+
1 row in set (0.08 sec)