MySQL基本查询语句及方法

基本查询语句及方法:

书写顺序:

  比如: select * from emp where id > 3 and id < 6;

执行顺序:

  from:先确定是哪张表 

    from 表名

  where:根据过滤条件筛选数据   

    where nane like ‘%要查询的数据%’

  select:再取出筛选出来的某些数据字段

    select mame,salary

  建表:

#  建一个新库,指定库建表

create database ku1;
use ku1;
create table emp(
  id int not null unique auto_increment,
  name varchar(20) not null,
  sex enum('male','female') not null default 'male', #大部分是男的
  age int(3) unsigned not null default 28,
  hire_date date not null,
  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,'20170301','张江第一帅形象代言',7300.33,401,1), #以下是教学部
('egon','male',78,'20150302','teacher',1000000.31,401,1),
('kevin','male',81,'20130305','teacher',8300,401,1),
('tank','male',73,'20140701','teacher',3500,401,1),
('owen','male',28,'20121101','teacher',2100,401,1),
('jerry','female',18,'20110211','teacher',9000,401,1),
('nick','male',18,'19000301','teacher',30000,401,1),
('sean','male',48,'20101111','teacher',10000,401,1),

('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
('丫丫','female',38,'20101101','sale',2000.35,402,2),
('丁丁','female',18,'20110312','sale',1000.37,402,2),
('星星','female',18,'20160513','sale',3000.29,402,2),
('格格','female',28,'20170127','sale',4000.33,402,2),

('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
('程咬金','male',18,'19970312','operation',20000,403,3),
('程咬银','female',18,'20130311','operation',19000,403,3),
('程咬铜','male',18,'20150411','operation',18000,403,3),
('程咬铁','female',18,'20140512','operation',17000,403,3)
;

 

where  基本查询:

  在我们刚接触mysql查询的时候,我们最好按照查询优先级顺序拼写我们的sql语句。

1.查询id大于等于3小于等于6的数据
    select * from emp where id >= 3 and id <= 6;
    select * from emp where id between 3 and 6;

    上述语句完全等价

# 2.查询薪资是20000或者18000或者17000的数据
    select id,name from emp where salary = 20000 or salary = 18000 or salary = 17000;
    select id,name from emp where salary in (20000,18000,17000);
# 3.查询员工姓名中包含o字母的员工姓名和薪资
  模糊匹配 like
    %:匹配多个任意字符
    _:匹配一个任意字符
    select name,salary from emp where name like '%o%';

# 4.查询员工姓名是由四个字符组成的员工姓名与其薪资
    select name,salary from emp where name like '____';

# 5.查询id小于3或者大于6的数据
    select * from emp where id < 3 or id > 6;
    select * from emp where id not between 3 and 6;

# 6.查询薪资不在20000,18000,17000范围的数据
    select id,name 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 is Null;
MySQL对大小写不敏感

group by  分组

  分组之后应该做到最小单位是组,而不应该再展示组内的单个数据信息,MySQL中分组之后

只能拿到分组的字段信息 无法直接获取其他字段信息;但是我们可以通过其他方法(聚合函数)简介的获取。

  

1.按部门分组

select * from emp group by post;   # 分组之后应该做到最小单位是组,而不应该再展示组内的单个数据信息,

这样语法有错误,应该报错,如果没报错是因为我们没有设置严格模式。

设置严格模式:

  show variables like '%mode%';
  set session 当前窗口有效
  set global 全局有效
  set global sql_mode="strict_trans_tables,only_full_group_by";  #  严格模式指令
select * from emp group by post;  # 报错   说id不在分组内
select id,name from emp group by post;  # 报错   说id不在分组内
select name from emp group by post;   #  报错  说name 也不在

select post from emp group by post;     #  设置严格模式之后只有post在。组就是默认最小单位了

设置严格模式分组之后,只能拿到分组依据对应的信息,也就是说你想要查看字段只能查看到分组的字段,

因为默认你按什么分组,也就默认了你分的哪个组就是最小单位了。组内的单个信息不会再展示给你
2.获取每个部门的最高工资 聚合函数 max min avg sum count
  select post,max(salary) from emp group by post;
给字段取别名
  select post as '部门',max(salary) as '最高工资' from emp group by post;
  select post '部门',max(salary) '最高工资' from emp group by post;
每个部门的最低工资
  select post,min(salary) from emp group by post; 
每个部门的平均工资
  select post,avg(salary) from emp group by post;
每个部门的工资总和
  select post,sum(salary) from emp group by post;
每个部门的人数
  select post,count(age) from emp group by post;
  select post,count(salary) from emp group by post;
  select post,count(id) from emp group by post;
  select post,count(post_comment) from emp group by post;
在统计分组内个数的时候 填写任意非空字段都可以完成计数,推荐使用能够唯一标识数据的字段,比如id字段

聚合函数 max min sum count avg只能在分组之后使用,如果一张表没有写group by默认所有的数据就是一组,

在分组的时候聚合函数会自动将每一个分组内的单个数据做想要的计算,无需我们考虑,它自己会比较

3.查询分组之后的部门名称和每个部门下所有的学生姓名
  select post,group_concat(name) from emp group by post;
  select post,group_concat('DSB',name) from emp group by post;   #  能做拼接操作
  group_concat()   #  能够拿到分组后每一个数据指定字段(可以是多个)对应的值

select post,group_concat(name,": ",salary) from emp group by post;   #  可以多个字段拼接

concat   # 不分组的时候用
  select concat("NAME: ",name),concat("SAL: ",salary) from emp;
小技巧:
  concat就是用来帮你拼接数据
  concat 不分组情况下使用
  group_concat 分组之后使用
查询每个员工的年薪12个月
  select name,salary*12 from emp;
刚开始查询表,一定要按照最基本的步骤,先确定是哪张表,再确定查这张表也没有限制条件,再确定是否需要分类,

最后再确定需要什么字段对应的信息。我们应该将每一步操作产生的结果都当成是一张新的表,然后再基于该表进行其他操作。

书写顺序,select ,from,where,group by

执行顺序,from,where,group by,select

having:

  跟where是一模一样的 也是用来筛选数据,但是having是跟在group by之后的,where是对整体数据做一个初步的筛选,

而having是对分组之后的数据再进行一次针对性的筛选。

统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门。
  select post,avg(salary) from emp where age > 30 group by post having avg(salary) > 10000;

执行顺序:from, where, group by, having,select 

distinct:去重

  select distinct id,age from emp;

  对重复的数据进行去重,但是必须得是重复的才行,就是必须数据是一模一样的才行,只要有一个个不重复就不会去除。

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

order by:排序

  默认是升序asc  也可以改成降序desc  

    select * from emp order by salary;   # 默认是升序排列
    select * from emp order by salary asc;  # 指定为升序排列
    select * from emp order by salary desc;  #  指定为降序排列
    select * from emp order by age,salary; # 先按照age做升序 age相同的情况下再按照salary做升序
    select * from emp order by age asc,salary desc; # 先按照age做升序 age相同的情况下再按照salary做升序

limit:现在展示的场景,限制数据展示的条数

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

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

查询工资最高的人的详细信息,可以先按照薪资排序,再用limit限制 只取一条:

  select * from emp order by salary desc limit 1;

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

   select * from emp where name regexp '^j.*(n|y)$';     #  取以j开头n或y结尾的数据。(regexp声明后面跟的是个正则)


多表查询:先建个库和两张表

# 建库 指定库名建表

create database ku2;
use ku1;
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;    #  左表一条记录与右表所有记录都对应一遍    被称为笛卡尔积

内连接(inner join)

左连接(left join)

右连接(right join)

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


1、内连接:只取两张表有对应关系的记录
  select * from emp inner join dep on emp.dep_id = dep.id;
  select * from emp inner join dep on emp.dep_id = dep.id where dep.name = "技术";

2、左连接: 在内连接的基础上保留左表没有对应关系的记录
  select * from emp left join dep on emp.dep_id = dep.id;

3、右连接: 在内连接的基础上保留右表没有对应关系的记录
  select * from emp right join dep on emp.dep_id = dep.id;

4、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录
  select * from emp left join dep on emp.dep_id = dep.id
  union
  select * from emp right join dep on emp.dep_id = dep.id;

子查询:将一张表的查询结果,作为另外一个sql语句的查询条件

  select name from dep where id = (select dep_id from emp where name = 'jason');

表的查询结果可以作为其他表的查询条件,也可以通过其别名的方式把它作为一张虚拟表去跟其它表做关联查询。

2.每个部门最新入职的员工
思路:先查每个部门最新入职的员工,再按部门对应上联表查询

select t1.id,t1.name,t1.hire_date,t1.post,t2.* from emp as t1
inner join
(select post,max(hire_date) as max_date from emp group by post) as t2
on t1.post = t2.post
where t1.hire_date = t2.max_date
;

可以给表起别名
可以给查询出来的虚拟表起别名
可以给字段起别名

关键字都是as

猜你喜欢

转载自www.cnblogs.com/sweet-i/p/11391490.html