mysql的一些基本语法练习

题号有点乱,不过不影响大局。首先你要有MySQL数据库,小编的是MySQL5.7。然后建两个表emp和dept

建表建议使用navicat这种工具极大地减少了脑细胞损耗量。navicat是一种数据库管理工具,首先要选择连接,选择MySQL

连接名填数据库名

 

emp表

 

dept表

dept表
d_id d_name d_duty
1 教学部 教学
2 公关部 公关

其他的属性可以自己加

  1. 分组查询

14.查询出d_id=1的一共有多少人?

select count(e_id) from emp where d_id=1;

 

15.查询出教学部一共有多少人?

1、首先查询教学部的id

select count(e_id) from emp where d_id=(select d_id from dept where d_name='教学部')

 

16.查询出每一个部门有多少人;

部门:  java组  大数据组  ui组  py组   

分组查询:  group by 列名

select d_id,count(e_id) from emp group by d_id;

 

17.查询出部门人数大于1个人的部门;

 

select d_id,count(e_id) from emp group by d_id  having count(e_id)>1;

 

where 和 having后面都是条件

where 后面的条件里面不是使用函数 ,having里面只能是函数

having一般和group by 一起使用;

    1. 排序

18.查询出所有的员工信息,并且按年龄从大到小排序;

排序函数:  order by 列名 desc   降序

order by 列名 [asc]  升序

 

select * from emp order by e_age desc;

    1. Limit 分页

19.查询从表里面第2条第3条记录;

 limit x,y       x表示从第几条开始  从0开始   y 表示查询多少条记录

 

select * from emp limit 1,2

 

查询第10到--第16条   limit 9,7

    1. 查询语法

查询的语法:

select */列名 [as 别名] from 表名 [as 别名]

  [where 条件]

[group by 列名]

[having 条件]

[order by 列名 desc/asc]

[limit ?,?]

 

    1. 多表操作

 

20.查询出所有的部门信息以及员工信息;

 

select * from emp,dept;

这样查查出来员工笛卡尔集

{A B}   {1  2}

{A1  A2  B1  B2}

 

21.查询出所有的与员工信息 (员工的所有信息和他所属部门的信息)

select * from emp e,dept d  where e.d_id=d.d_id;

等值连接

 

22.查询出所有的与员工信息 (员工的所有信息和他所属部门的信息)

 

左连接    

左表  left join 右表 on 条件

 

左外连接

左表 LEFT OUTER JOIN 右表 on 条件

 

查询出来的是左表里面的所有数据以及右表里面符合条件的数据;

select * from emp e  left  join dept d on e.d_id=d.d_id;

 

右连接

查询出来的是右表的所有数据和左表里面符合条件的数据

select * from emp e right join dept d on e.d_id=d.d_id;

 

 

23.如果3张表

select * from b1 left join b2 on b1.xxx=b2.xxx left join b3 on b1.xxx=b3.xxx   left join ...

 

24.查询出小明的所属信息

select * from emp e left join dept d on e.d_id=d.d_id  where e.e_name="小明"

 

25.查询出小明的姓名,年龄以及部门名称;

select e.e_name ,e.e_age ,d.d_name from emp e left join dept d on e.d_id=d.d_id where e.e_name="小明"

 

 

1.查询员工表里面的所有信息;

select * from emp;

* 不建议使用

 select e_id,e_name,e_age,e_date,d_id from emp;

 

 

2.查询表里面的所有的姓名和年龄信息;

select e_name , e_age from emp;

 

3.查询表里面的姓名和年龄 并且使用别名;   

列名  [as]  别名

 

select e_name  姓名 , e_age as 年龄 from emp;

 

 

where  条件

where 条件里面可以使用  >  <  =   >=  <=  <>不等于

 

4.查询出年龄大于12的员工

select * from emp where e_age>12;

 

5.查询出年龄在11-13之间的员工

and 表示并且

 select * from  emp where e_age>11  and e_age<13;

 

between ...  and  ...   在。。。到。。。直接

select * from emp where e_age  between 11 and 13;

 

6.查询出年龄等于11的员工

select * from emp where e_age=11;

 

7.查询出年龄为 11  或者 12 的学生

or 或者

select * from emp where e_age=11 or e_age=12;

 

in(x,y,z)  或者 x 或者 Y ...

select * from emp where e_age in(11,12);

 

 

模糊查询

where 字段名 like xxx;

%小   以小结束的

小%    以小开头的

%小%  只要包含小的

_小%  第二位是小的

_ _ 小%  第三位是小的

 

8.查询出名字里面含有小的所有员工

select * from emp where e_name like "%小%";

 

9.查询出名字里面不含小的

select * from emp where e_name not like "%小%";

 

聚合函数

count()  统计

sum() 求和

avg() 求平均数

max() 最大的

min() 最小的

 

10.统计出一共有多少个学生;

select count(e_id) from emp;

 

11.求出emp的平均年龄

 

select avg(e_age) from emp;

 

12.求出最大的年龄

select max(e_age) from emp;

 

13.查询出最大年龄的员工信息;

 

select * from emp where e_age=(select max(e_age) from emp);

 

子查询

一个查询语句的结果是另一个查询语句的条件

 

1.查询出最大的年龄

select max(e_Age) from emp

 

2.查这个年龄的员工的信息

select * from emp  where e_age=(select max(e_age) from emp)

 

14.查询出d_id=1的一共有多少人?

select count(e_id) from emp where d_id=1;

 

15.查询出教学部一共有多少人?

1、首先查询教学部的id

select count(e_id) from emp where d_id=(select d_id from dept where d_name='教学部')

 

16.查询出每一个部门有多少人;

部门:  java组  大数据组  ui组  py组   

分组查询:  group by 列名

select d_id,count(e_id) from emp group by d_id;

 

17.查询出部门人数大于1个人的部门;

 

select d_id,count(e_id) from emp group by d_id  having count(e_id)>1;

 

where 和 having后面都是条件

where 后面的条件里面不是使用函数 ,having里面只能是函数

having一般和group by 一起使用;

 

18.查询出所有的员工信息,并且按年龄从大到小排序;

排序函数:  order by 列名 desc   降序

order by 列名 [asc]  升序

 

select * from emp order by e_age desc;

 

19.查询从表里面第2条第3条记录;

 limit x,y       x表示从第几条开始  从0开始   y 表示查询多少条记录

 

select * from emp limit 1,2

 

查询第10到--第16条   limit 9,7

 

查询的语法:

select */列名 [as 别名] from 表名 [as 别名]

  [where 条件]

[group by 列名]

[having 条件]

[order by 列名 desc/asc]

[limit ?,?]

 

一起在知识的海洋里遨游吧。

猜你喜欢

转载自blog.csdn.net/qq_42363342/article/details/81707564