失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > mysql 子表 关联查询语句_MySQL-基本查询语句及方法 连表和子查询

mysql 子表 关联查询语句_MySQL-基本查询语句及方法 连表和子查询

时间:2019-02-04 12:14:36

相关推荐

mysql 子表 关联查询语句_MySQL-基本查询语句及方法 连表和子查询

一、基本查询语句

create table emp(

id intnotnull unique auto_increment,

name varchar(20) notnull,

sex enum('male','female') not null default 'male', #大部分是男的

age int(3) unsigned not null default 28,

hire_date datenotnull,

post varchar(50),

post_comment varchar(100),

salary double(15,2),

office int,#一个部门一个屋子

depart_id int

);#插入记录#三个部门:教学,销售,运营

insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values

('jason','male',18,'0301','张江第一帅形象代言',7300.33,401,1), #以下是教学部

('egon','male',78,'0302','teacher',1000000.31,401,1),

('kevin','male',81,'0305','teacher',8300,401,1),

('tank','male',73,'0701','teacher',3500,401,1),

('owen','male',28,'1101','teacher',2100,401,1),

('jerry','female',18,'0211','teacher',9000,401,1),

('nick','male',18,'19000301','teacher',30000,401,1),

('sean','male',48,'1111','teacher',10000,401,1),

('歪歪','female',48,'0311','sale',3000.13,402,2),#以下是销售部门

('丫丫','female',38,'1101','sale',2000.35,402,2),

('丁丁','female',18,'0312','sale',1000.37,402,2),

('星星','female',18,'0513','sale',3000.29,402,2),

('格格','female',28,'0127','sale',4000.33,402,2),

('张野','male',28,'0311','operation',10000.13,403,3), #以下是运营部门

('程咬金','male',18,'19970312','operation',20000,403,3),

('程咬银','female',18,'0311','operation',19000,403,3),

('程咬铜','male',18,'0411','operation',18000,403,3),

('程咬铁','female',18,'0512','operation',17000,403,3)

;#ps:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk

前期准备

1.select# 拿出筛选出来的数据中的某些字段

2.from# 确定到底是哪张表

3.where# 约束条件,对整体数据进行初步筛选(放在分组前)

#1.查询id大于等于3小于等于6的数据

select id,name from emp where id >= 3 and id <= 6;

select* from emp where id between 3 and 6;#2.查询薪资是20000或者18000或者17000的数据

select * from emp where salary = 20000 or salary = 18000 or salary = 17000;

select* from emp where salary in (20000,18000,17000); #简写

#3.查询员工姓名中包含o字母的员工姓名和薪资#在你刚开始接触mysql查询的时候,建议你按照查询的优先级顺序拼写出你的sql语句

"""先是查哪张表 from emp

再是根据什么条件去查 where name like ‘%o%’

再是对查询出来的数据筛选展示部分 select name,salary"""select name,salaryfrom emp where name like '%o%';#4.查询员工姓名是由四个字符组成的员工姓名与其薪资

select name,salary from emp where name like '____';

select name,salaryfrom emp where char_length(name) = 4;#5.查询id小于3或者大于6的数据

select * from emp where id not between 3 and 6;#6.查询薪资不在20000,18000,17000范围的数据

select * from emp where salary not in (20000,18000,17000);#7.查询岗位描述为空的员工名与岗位名 针对null不能用等号,只能用is

select name,post from emp where post_comment = NULL; #查询为空!

select name,post from emp where post_comment isNULL;

select name,postfrom emp where post_comment is not NULL;

View Code

4.group by# 分组

4.1一般和聚合函数一起用max、min、avg、sum、count

#2.获取每个部门的最高工资#以组为单位统计组内数据>>>聚合查询(聚集到一起合成为一个结果)#每个部门的最高工资

select post,max(salary) fromemp group by post;#每个部门的最低工资

select post,min(salary) fromemp group by post;#每个部门的平均工资

select post,avg(salary) fromemp group by post;#每个部门的工资总和

select post,sum(salary) fromemp group by post;#每个部门的人数

select post,count(id) from emp group by post;

View Code

4.2 给字段起别名 :字段名 as '别名'

4.3 严格模式下,分组后不能够直接再插到单个数据信息,只能获取到组名

set global sql_mode="strict_trans_tables,only_full_group_by";#重新链接客户端

select * from emp group by post; #报错

select id,name,sex from emp group by post; #报错

select post from emp group by post; #获取部门信息

View Code

所以可以用group_concat 字符拼接

#3.查询分组之后的部门名称和每个部门下所有的学生姓名#group_concat(分组之后用)不仅可以用来显示除分组外字段还有拼接字符串的作用

select post,group_concat(name) fromemp group by post;

select post,group_concat(name,"_SB") fromemp group by post;

select post,group_concat(name,":",salary) fromemp group by post;

select post,group_concat(salary)fromemp group by post;#4.补充concat(不分组时用)拼接字符串达到更好的显示效果 as语法使用

select name as 姓名,salary as 薪资 fromemp;

select concat("NAME:",name) as 姓名,concat("SAL:",salary) as 薪资 fromemp;#补充as语法 即可以给字段起别名也可以给表起

select emp.id,emp.name from emp as t1; #报错 因为表名已经被你改成了t1

select t1.id,t1.name from emp as t1;

View Code

小技巧:

conca t就是用来帮你拼接数据

concat 不分组情况下使用

group_concat 分组之后使用

5.having# 对分组之后数据再进行一次正对性筛选(放在分组后)

having的语法格式与where一致,只不过having是在分组之后进行的过滤,即where虽然不能用聚合函数,但是having可以!

1、统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门

select post,avg(salary)fromemp

where age>= 30group by post

having avg(salary)> 10000;#如果不信你可以将having取掉,查看结果,对比即可验证having用法!

#强调:having必须在group by后面使用

select * from emp having avg(salary) > 10000; #报错

View Code

6.distinct# 去重

#对有重复的展示数据进行去重操作

select distinct post from emp;

View Code

7.order by# 排序

默认是升序 asc

也可以变成降序 desc

select * from emp order by salary asc; #默认升序排

select * from emp order by salary desc; #降序排

select* from emp order by age desc; #降序排

#先按照age降序排,在年轻相同的情况下再按照薪资升序排

select * fromemp order by age desc,salary asc;#统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序

select post,avg(salary) fromemp

where age> 10group by post

having avg(salary)> 1000order by avg(salary)

;

View Code

8.limit# 限制数据的条数

select * from emp limit 5; #只展示数据的五条

select * from emp limit 5,5;"""当limit只有一个参数的时候 表示的是只展示几条

当limit有两个参数的时候 第一个参数表示的起始位置 第二个参数表示从起始位置开始往后展示的条数"""

View Code

9、正则

# 在编程中 只要看到reg开头的 基本上都是跟正则相关

select * from emp where name regexp '^j.*(n|y)$';

select * from emp\G; 当表字段特别多时,结果的排版可能会出现混乱的现象 可以在查询语句加\G来规范查询结果

ps:MySQL对大小写不敏感

执行顺序:from→where→group by→having→select→distinct

二、连表

#建表

create table dep(

id int,

name varchar(20)

);

create table emp(

id int primary key auto_increment,

name varchar(20),

sex enum('male','female') not null default 'male',

age int,

dep_id int

);#插入数据

insert into dep values

(200,'技术'),

(201,'人力资源'),

(202,'销售'),

(203,'运营');

insert into emp(name,sex,age,dep_id) values

('jason','male',18,200),

('egon','female',48,201),

('kevin','male',38,201),

('nick','female',28,202),

('owen','male',18,200),

('jerry','female',18,204)

;

建表

引子:

select * from emp,dep; #左表一条记录与右表所有记录都对应一遍>>>笛卡尔积

#将所有的数据都对应了一遍,虽然不合理但是其中有合理的数据,现在我们需要做的就是找出合理的数据

#查询员工及所在部门的信息

select * from emp,dep where emp.dep_id =dep.id;#查询部门为技术部的员工及部门信息

select * from emp,dep where emp.dep_id = dep.id and dep.name = '技术';

笛卡尔积

1.so 将两张表关联到一起的操作,有专门对应的方法

inner join (内连接:只取两张表有对应关系的记录)

left join(左连接:在内连接的基础上保留左表没有对应关系的记录)

right join(右连接:在内连接的基础上保留右表没有对应关系的记录)

union(全连接:在内连接的基础上保留左、右面表没有对应关系的的记录)

# 只要将左连接和右连接的sql语句 加一个union就变成全连接

例:

select *from emp left join dep on enp.dep_id = dep_id

union

select * from emp right join dep on emp.dep_id = dep.id;

三、子查询

即:将一个查询语句的结果用括号括起来当作另外一个查询语句的条件去用

#1.查询部门是技术或者人力资源的员工信息

"""先获取技术部和人力资源部的id号,再去员工表里面根据前面的id筛选出符合要求的员工信息"""select* from emp where dep_id in (select id from dep where name = "技术" or name = "人力资源");

如果觉得《mysql 子表 关联查询语句_MySQL-基本查询语句及方法 连表和子查询》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。