MySQL查询语句(DQL语句)

MySQL查询语句

  • 基础查询
  • 条件查询
    按条件表达式
    按逻辑表达式
    模糊查询
  • 排序查询
  • 常见函数
    单行函数
    分组函数
  • 分组查询
  • 连接查询(多表查询)
  • 子查询
  • 分页查询

----------- 要数据库表信息的可以私聊我---------

基础查询

1、查询表中单个字段

select 查询列表 from 表名

2、查询多个字段

select 查询列表,查询列表,...,查询列表 from 表名

3、查询所有字段

select * from 表名

4、查询常量值

select 100;
select 'jack';

5、查询表达式

select 100*2;
select 50%2;     	**% 求余数**

6、查询函数

select version();

7、起别名

as 来给字段起别名 好处: 1、便于理解 2、如果查询的字段有重名的情况,可以使用别名来区分 3、as可以省略掉

8、去重(distinct)

select DISTINCT department_id from employees;

9、+号

Java中:1、运算 2、拼接 MySQL中:运算 select 100+50; – 两个操作算都是数值型的,则做加法运算
select ‘123’+100; – 只有一边为数值型,试图将字符型转换为数值型,若 转换成功则运算 select
‘a’+100; – 若转换成功,则将字符型转换为() select null + 100; –
只要其中一方为null,其结果肯定为null

条件查询

语句:select 查询列表 from 表名 where 筛选条件;

分类:
1、按条件表达式筛选 > >= < <= = != <>
2、按逻辑表达式筛选 &&(and) ||(or) ! (not)
3、模糊查询
1>、like 2>、between and 3>、in 4>、is null

按条件表达式

例如:1、查询工资大于12000的员工信息

select * from employees where salary>12000;

按逻辑表达式

例如:查询工资在10000到15000之间的员工姓、工资

select last_name,salary 
from employees 
where salary>=10000 and salary<=15000;

模糊查询

like:一般和通配符搭配使用
通配符:
% —>任意多个字符,包含0个字符
_ —>任意单个字符

例1:查询员工last_name中第三个为e,第五个字符为a的员工

select *
from employees
where last_name like '__e_a%';

例2:查询员工last_name中第二个为_ 的员工

select *
from employees
where last_name like '_\_%';

例3:查询员工编号在100-120之间的员工

select *
from employees
where employee_id>=100 and employee_id <=120;
----------------------替换---------------------
select *
from employees
where employee_id between 100 and 120;

between and
包含两个临界值,两个临界值不能调换位置

例4:查询员工的工种编号IT_PROG、AD_VP中的员工信息

select *
from employees
where job_id = 'IT_PROG' or job_id = 'AD_VP';
#替换
select *
from employees
where job_id in('IT_PROG','AD_VP');

in
in列表中的值类型必须是一致的或者是兼容的
in列表中不支持通配符

例5:查询没有奖金率的员工信息

select *
from employees
where commission_pct is null;

is null
=和<> 不能用于判断null值
is null和is not null 可以判断null

注意:
<=> 安全等于
is null 和 <=> 区别
is null --> 仅仅只能判断空值
<=> --> 既可以判断null值,也可以判断数值,可读性差

排序查询

语法:select 查询列表 from 表名 [where 筛选条件] order by 排序的字段
特点:
1.acs—>代表的是升序,可以省略不写
desc—>代表的是降序
2.order by 可以支持单个字段、多个字段、别名、表达式、函数
3.order by 在查询语句最后,除了limit(分页)

例如:案例1:查询员工信息以工资进行降序

select *
 from employees
 order by salary desc;

按照别名:
案例2:查询员工信息,并按照年薪进行降序

 select *,salary*12 as '年薪'
 from employees
 order by 年薪 desc;

ifnull(s1,s2) —>判断s1是否为null,若为空,则使用s2来代替null

 select employee_id,(1+ifnull(commission_pct,0))*salary*12 '年薪'
from employees
order by 年薪 desc

按照函数排序
案例3:查询员工的last_name,并按照姓的长度进行排序

select last_name,length(last_name) as '长度'
from employees
order by 长度;

按照多个字段进行排序
案例4:查询员工信息,并且先按照工资进行降序,在按员工编号排序

select *
from employees
order by salary desc,employee_id

常见函数

分类:
	1、单行函数
		concat 	length 	ifnull
	2、分组函数
		功能:做统计使用的,又称为统计函数、聚合函数
		count  max  min
		
	单行函数:
		1.字符函数:
			length:获取字节个数
			concat:拼接
			substr:截取
			trim  :去两端空格
			upper :转大写
			lower :转小写
			replace:替换
			lpad(str,len,padstr) 左填充
			rpad(str,len,padstr) 右填充
			instr(str,substr)    返回子字符串第一次出现的索引位置,如果找不到返回0
		2.数学函数
			round:四舍五入
			floor:向下取整
			ceil :向上取整
			mod  :取余
			truncate:截断
		3.日期函数
			now    :显示当前日期+时间
			curdate:显示当前日期
			curtime:显示当前时间
			year	 :年
			month	 :月
			day		 :日
			hour	 :时
			minute   :分
			second   :秒
			str_to_date:转换日期(字符串转换为date)
			date_format:解析日期(date转换为字符串)
		4.其他函数
			version:查看版本
			database:查看当前使用的数据库
			user:查看当前登录的用户
		5.控制函数
			if  :多分支
			case:多分支

字符串函数

1、length 获取长度

select length(last_name) from employees;

2、concat 拼接

select concat(`first_name`,'-',`last_name`) as '姓名' from employees

3、upper lower 转换大小写

select upper(last_name) from employees;
select lower(last_name) from employees;

4、substr substring 截取
注意:索引从1开始

select substr('法外狂徒',2);		-- 截取从指定索引位置后面开始的
select substr('法外狂徒',2,2);	-- 截取从指定索引位置开始多少长度
select substring('法外狂徒张三',3);

5、instr —> indexOf
返回子字符串第一次出现的索引位置,如果找不到返回0

select instr('法外狂徒张三','张三');

6、trim 去两端空格
7、lpad rpad 用指定字符实现左右填充指定长度

法外狂徒张三,狂徒 的长度<12,就用*去填充
如果>12,只用显示前12个字符

select lpad('法外狂徒张三,狂徒',12,'*');

8、replace 替换

select replace('法外狂徒张三','张三','罗翔');

数学函数

四舍五入

select round(5.5);
select round(-2.5);

向下取整

select floor(2.9);

向上取整

select ceil(2.000001);

取余

select mod(10,3);   --  10%3

截断

select truncate(1.99996666,2);

日期函数

显示当前日期+时间

select now();

显示当前日期

select curdate();

显示当前时间

select curtime();

例如:

select * from employees
where hiredate = '1992-4-3';

select * from employees
where hiredate = str_to_date('1992-4-3','%Y-%c-%d');

select STR_TO_DATE('1994-1-01','%Y-%m-%d');

select date_format(now(),'%Y年-%m月-%d日');

其他函数

查看版本

select version();

查看当前使用的数据库

select database();

查看当前登录的用户

select user();

控制函数

if

select if(10<3,'大','小')
`IF`(expr1,exxpr2,expr3)

case
case 要判断的字段或表达式
when 常量1 then 要显示的值1或语句1
when 常量2 then 要显示的值2或语句2

else 要显示的值n或者语句n
end

例如:查询员工的工资,要求:部门=30的涨薪1.2倍 部门=50涨薪1.3倍 剩下部门的还是原工资

select employee_id,last_name,salary,department_id,
case department_id
when 30 then salary*1.2
when 50 then salary*1.3
else salary
end '新工资'
from employees

case 当成if-else使用,case后就不需要加任何内容

select salary,
case 
when salary>15000 then 'A'
when salary>10000 then 'B'
when salary>5000  then 'C'
else 'D'
end '显示级别'
from employees

分组函数

功能:用于统计使用,又称统计函数、聚合函数
分类:
sum、max、min、count、avg
特点:
1.sum、avg一般用于处理数值类型
max、min、count可以处理任意类型
2.以上分组函数都能忽略null值
3.可以和distinct搭配使用实现去重的效果

分组函数的使用

select max(salary) from employees
select min(salary) from employees

select count(salary) from employees
select sum(salary) from employees
select avg(salary) from employees

注意:
①效率

存储引擎:是mysql数据的核心组件
MySQL默认的存储引擎是:InnoDB
count()和count(1) 效率差不多,但要比count(字段)高一些
MySam引擎下:count(
)的效率高
②和分组函数一起查询的字段是有限制的

分组查询

语法:
select 查询列表 from 表名 [where 筛选条件] group by 分组的字段 [order by 排序字段]
特点:
1.可以按照单个字段,多个字段分组(多个字段使用逗号隔开)
2.可以支持排序
3.筛选分类:
①分组前筛选 用where
②分组后筛选 用having

例如:
案例1:查询每一个部门的员工个数

select count(*),department_id from employees group by department_id 

案例2:查询每个工种下的平均工资

select avg(salary),job_id from employees group by job_id

案例3:查询邮箱中包含a字符的每个部门下的最高工资

select max(salary),department_id,email from employees 
where email like '%a%' 
group by department_id

案例4:查询哪一个部门下员工个数>5

①查询每个部门下的员工个数**
select department_id,count(*)
from employees
group by department_id
**②筛选刚才①的结果  人数>5**
having count(*) > 5

案例5:领导编号>102的每个领导手下最低工资大于5000的领导编号和最低工资

select min(salary),manager_id
from employees
where manager_id>102
group by manager_id
having min(salary)>5000
order by min(salary) desc

多个字段进行分组
案例6:查询每个工种每个部门的最低工资并排序

select min(salary),job_id,department_id
from employees
group by job_id,department_id
order by min(salary);

连接查询(多表查询)

连接查询:又称多表查询,当查询的字段来自于多个表,就会用到连接查询

案例:查询员工名和对应的部门名

select last_name,department_name    
from employees,departments;
(这种不对)
上面会出现笛卡尔乘积,employees表中107条数据,departments表中27条数据  
结果=107*27
出现原因多表之间没有有效的连接条件

分类:
SQL92和SQL99
92:内连接
99:内连接、外连接(左外、右外)、交叉连接【推荐】

语法:
	select 查询字段
	from1
	[inner | left outer | right outer] join2 on 连接条件
	[inner | left outer | right outer] join3 on 连接条件
	[where 分组前筛选条件]
	[group by 分组条件]
	[having 分组后筛选条件]
	[order by 排序的字段~~删除线格式~~ ]

案例:查询员工和对应的部门名

select last_name,department_name    
from employees,departments
where employees.department_id = departments.department_id;

**注意:**若查询的字段在多表中都存在,则需要指明此字段从哪张表查询的
为表名来起别名,用来区分多个重名的字段
若为表起了别名,则查询字段不能在用原来的表名

例如将上面的案例起别名

select last_name,department_name    
from employees e,departments d
where e.department_id = d.department_id;
-- employees as e,其中 as可以省略

可以进行分组、排序
例如:查询每个城市的部门个数

select count(*) '人数',job_title
from jobs j,employees e
where j.job_id = e.job_id
group by j.job_id
order by 人数 desc;

案例:使用左(右)外连接
查询员工和对应的部门名

select last_name,department_name    
from employees e,departments d
where e.department_id = d.department_id;

替换:使用左连接

select last_name,department_name  
from employees e
join departments d
on e.department_id = d.department_id;

left outer join 左外连接
right outer join 右外连接
特点:
1、外连接的查询结果为主表的所有记录
如果从表中没有和主表匹配的,则实现null
如果从表中有和主表匹配的,则显示匹配的值

外连接 = 内连接结果 + 主表中有而从表中没有的记录

2、左外连接,left join 左边是主表
右外连接,right join 右边是主表

子查询

出现在其他语句中的select语句,称为子查询或内查询
外部的查询语句,称为主查询或外查询

分类:
	按子查询出现的位置:
		select:标量子查询
		from:表子查询
		where或having:标量子查询(单行)、列子查询(多行)、行子查询
		
	按结果集2行列数不同:
		标量子查询(结果集只有一行一列)
		列子查询(结果集只有一列多行)
		行子查询(结果集有一行多列)
		表子查询(结果集是多行多列)
	
	1.where或having后面
		特点:
				①子查询放在小括号
				②子查询一般放在条件的右边
				③子查询是优先于主查询执行的,主查询的结果.....未写完

where 或 having 后面

**例1:谁的工资比Abel高**
①查询Able的工资是多少
select last_name,salary from employees where last_name = 'Abel';
②工资>①的结果
select *
from employees
where salary > (
	select salary 
	from employees 
	where last_name = 'Abel'
);

例2:返回job_id与141号员工相同的,
salary比143号员工多的员工姓名,job——id,工资

141号员工的job_id
select job_id
from employees
where employee_id = 141143号员工的salary
select salary
from employees
where employee_id = 143
③查询员工姓名,job_id,工资,要求job_id=①,salary >select last_name,job_id,salary
from employees
where job_id = (
	select job_id
	from employees
	where employee_id = 141
) and salary > (
	select salary
	from employees
	where employee_id = 143
)

案例3:返回公司中最低工资的员工的last_name,job_id,salary

①查询最低工资
select min(salary)
from employees
②工资salary =select last_name,job_id,salary
from employees
where salary = (
	select min(salary)
	from employees
)
------------------------
------- 不行-----------
select last_name,job_id,salary       错的,这种不行
from employees						 错的,这种不行
where salary = (min(salary))		 错的,这种不行

例4:查询最低工资大于50号部门的最低工资的部门id和最低工资

50号部门最低工资
select min(salary)
from employees
where department_id = 50
②查询每个部门的最低工资
select min(salary),department_id
from employees
group by department_id
③在②的基础上进行筛选,满足min(salary) > ①的结果
select department_id,min(salary)
from employees
group by department_id
having min(salary) > (
	select min(salary)
	from employees
	where department_id = 50
)	

列子查询(多行)

例1:返回location_id是1400或1700的部门中所有员工last_name

①location_id是14001700的部门编号
select DISTINCT department_id
from departments
where location_id = 1400 or location_id = 1700
-------- 替换 -------------
select DISTINCT department_id
from departments
where location_id in(1400,1700)

②查询员工last_name,要求部门号是①结果中某一个
select last_name
from employees
where department_id = any (
  select DISTINCT department_id
  from departments
  where location_id in(1400,1700)
)

all 
any  表示其中任意一个

**例2:返回其工种中比job_id为’IT_PROG’ 工种中任意工资低的员工的员工号,last_name,salary,job_id **

①查询job_id为'IT_PROG'的工资
select salary
from employees
where job_id = 'IT_PROG'
②员工号,last_name,salary,job_id  要求salary<①结果中的任意一个
select employee_id,last_name,salary,job_id
from employees
where salary < any(
  select salary
  from employees
  where job_id = 'IT_PROG'
)
----------------替换----------------
select employee_id,last_name,salary,job_id
from employees
where salary < (
  select max(salary)
  from employees
  where job_id = 'IT_PROG'
)

例3:返回其工种中比job_id为’IT_PROG’ 工种中所有工资低的员工的员工号,last_name,salary,job_id

select employee_id,last_name,salary,job_id
from employees
where salary < all(
  select salary
  from employees
  where job_id = 'IT_PROG'
)
----------------替换----------------
select employee_id,last_name,salary,job_id
from employees
where salary < (
  select min(salary)
  from employees
  where job_id = 'IT_PROG'
)

行子查询(结果一行多列)

例:查询员工编号最小并且工资最高的员工信息

①查询最小的员工编号
select min(employee_id)
from employees
②查询工资最高
select max(salary)
from employees
③
select *
from employees
where employee_id = (
  select min(employee_id)
  from employees
) and salary = (
  select max(salary)
  from employees
) 
----------------替换----------------
select *
from employees
where (employee_id,salary) = (
  select min(employee_id),max(salary)
  from employees
)

select后面 : 标量子查询

例:查询员工编号为102的所在部门名

多表连接查询
select department_name
from departments d
inner join employees e 
on d.department_id = e.department_id
where employee_id = 102
order by employee_id asc;

子查询
select (
  select department_name
  from departments d
  inner join employees e 
  on d.department_id = e.department_id
  where employee_id = 102
  order by employee_id asc
) as '部门名'

from 后面 将子查询的结果充当为一张表

注意:子查询的结果当成表,一定要添加一个别名

例:查询每个部门的平均工资的工资等级

①查询每个部门的平均工资
select department_id,avg(salary)
from employees
group by department_id
②在①的基础上查等级
select department_id as '部门编号',avg(salary) as '平均工资',
case 
when avg(salary) > 8000 then 'A'
when avg(salary) > 7000 then 'B'
when avg(salary) > 6000 then 'C'
else 'D'
end as '工资等级'
from employees
group by department_id
----------------替换----------------
select *,
case 
when ag > 8000 then 'A'
when ag > 7000 then 'B'
when ag > 6000 then 'C'
else 'D'
end as '工资等级'
from (
  select department_id,avg(salary) ag
  from employees
  group by department_id
) as grades

分页查询

应用场景:
	实际Web项目中根据用户的需求提交对应的分页查询SQL语句
	
语法:
	select 查询列表
	from 表
	[where]
	[group by]
	[having]
	[order by]
	limit [起始的条目索引],条目数
	
特点:
	1、起始索引从0开始的
	2、limit语句放在查询语句的最后
	3、假如:要显示的页数:page		每页显示的条目数:sizePerPage
			公式:select * from 表 limit (page-1)* sizePerPage , sizePerPage

查询员工表中前5条数据

select * from employees
limit 0,5
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值