【2023】数据库sql增删改查执行命令汇总(配合详细举例)

一、单表查询

数据查询语言,专门用于查找表中的数据。

select * from emp;

关键字

 select * from 表名

查询表中的所有数据

source 导入文件的文件地址\文件名称

导入dump数据(结尾不能加" ; ")

mysql的数据备份文件称为dump文件

  1. 四则运算

四则运算+ ,-, *, /      //查询还可以配合运算符一起查询输出

 2、去重

关键字:

distinct。

只有当所有列的数据都一样,才去重。

语法:select distinct 列名 from 表名;

#查询当前有哪些工作岗位。

        select distinct job from emp;

 3、where:过滤

语法:

        select             //查询

        列名1,列名2…

        from          //定位什么表

        表名

        where           //过滤约束条件

        过滤条件。

过滤条件:比较运算符:

>、<、<=、>=、!=、<>

  1. 执行过程:先执行from定位表位置,在执行where进行条件过滤,最后执行select查询输出

过滤后的结果集.

  1. 虚表:结果集在内存运行时产生的一张虚拟的表.

虚标是用来存放结果集的

结果集就是虚标中显示的那一部分表格

4、is null:为空

语法:

        select             //查询

        列名1,列名2…

        from          //定位什么表

        表名

        where           //过滤约束条件

        列名 is null        //表示该列名为空

示例:查询没有奖金的员工信息。

        select * from emp where comm is null;

5、is not null:不为空

not 也可以和其他的关键字连着使用  为“不为”什么的意思

语法:

        select             //查询

        列名1,列名2…

        from          //定位什么表

        表名

        where           //过滤约束条件

        列名 is  not null        //表示过满足该列名不为空

示例:查询有奖金的员工信息。

        select * from emp where comm is not null;

6、and:并且

语法:

        select             //查询

        ename as 姓名,       //as表示别名,ename在输出结果集时会显示姓名这个别名,只是在结果集

        sal as 工资,             //  显示,不会真正改变表的表头

        from          //定位什么表

        表名

        where           //过滤约束条件

        列名 条件 and 列名 条件       //表示满足前面一个条件并且满足后面一个条件

示例:查询工资大于1000 并且可以获取奖金的员工的姓名,工资,奖金。

select
        ename as 姓名, sal 工资, comm 奖金
from  emp 
where   
        sal > 1000  and  comm is not null;
7、or 或者

语法:

        select             //查询

        列名1,列名2…

        from          //定位什么表

        表名

        where           //过滤约束条件

        列名 条件1 or 列名 条件2       //表示查询输出满足条件1的或者满足条件2的

示例:查询从事销售工作,或者工资大于等于2000的员工编号,工作,入职时间,工资。

select
        empno,job,hiredate,sal
from   emp
where
        job = 'salesman'  or  sal>=2000;

8、not:非,去反

语法:

        select

        列名1,列名2….

        from

        表名

        where

        not 列名 条件;

示例:查询从事非销售工作,并且工资不小于1500的员工的编号,姓名,职位,工资,入职时间。

select

        ename,job,sal,hiredate

from   emp

where

not (job="salesman")     and    not (sal<1500);

9、between and:区间

语法

        select

        列名1,列名2...

        from

        表名

        where

        列名 between 条件 and 条件。

注意:between and 等同于: >=  and <=,//字符也可判断,会判断首字母的值的大小

示例:查询工资大于1500 但是小于3000的员工的信息

        select

        *

        from

        emp

        where

        sal between 1500 and 3000;

10、in:包含,包括的含义

语法:

        select

        列名1,列名2...

        from

        表名

        where

        列名 in(条件1,条件2...)。

示例:查询编号为7566、7900、7369、8888的员工的信息。   

        select

        *

        from

        emp

        where

        empno in(7566,7900,7369,8888);

11、not in:不包含

语法:

        select

        列名1,列名2...

        from

        表名

        where

        列名 not in(条件1,条件2...)。

示例:查询不是SMITH、MARTIN、ADAMS的员工信息。

        select

        *

        from

        emp

        where

        ename not in('smith','martin','adams');

12、like:模糊匹配

需要结合2个通配符使用

  1. _匹配任意一个字符
  2. %匹配任意长度的字符

语法:

        select

        列名1,列名2...

        from

        表名

        where

        列名 like 条件

示例:查询名字的第三个字符是A的员工的信息。

        select

        *

        from

        emp

        where

        ename like '__a%';

    //__有两个'_ _'表示前两个字符任意一个字符,第三个字符要是A,后面%的字符随便

示例:查询1989年入职的员工的信息。

        select

        *

        from

        emp

        where

        hiredate like '1988%';

13、order by:排序

语法:如果有where,order by必须在where后面,

        select

        列名1,列名2…

        from  表名

        where

        过滤条件

        order by

        排序列1,排序列2….   asc | desc;           //  ‘|’表示或

排序规律会按当排序列1相同,才会按排序列2的序列去排,依次类推

asc:默认的,按照升序排序,可不写

desc:按照降序排序

示例1:查询员工的信息,按照工资由低到高进行排序。

        select

        *

        from     emp

        order by  sal desc;

示例2:查询10号部门员工的信息,按照工资由高到低进行排序。

        select

        *

        from   emp

        where    deptno=10

        order by   sal;

示例3:查询所有员工年薪【(sal+comm)*12】,按照年薪由高到低排序

        select

        ename,

        (sal+ifnull(comm,0)) * 12  年薪  

        from   emp

        order by  年薪 desc;

sql 执行过程:

from -->where-->select -->order by

14、limit(startIndex,size):分页

语法:

        select

        列名1,列名2…

        from

        表名

        where

        过滤条件

        order by

        排序列1,排序列2….   asc

        limit startIndex ,size;

startIndex:开始下标

size:取几条

示例:

#获取构造少于1500的前3条数据的员工信息

        select

        *

        from

        emo

        where

        sal<1500;         //获取工资小于1500的表

        limit (1-1)*3,3;    

分页算法:

需求:每页显示3条数据,一共显示多少页?

查询第一页数据

查询到最后一页的数据

当前页:默认1

总页数:总数据量/每页显示的条目数。14/3=5

//计算机调用分页时,最终底层是会按分页算法调用

计算开始下标:(当前页-1*每页显示的条目数;

虚表:

        虚标是SQL在运行过程中产生的众多张表,结果集就是虚标中的一张显现出来的表

二、多表查询

1、等值连接

语法:select

        列名…

        from

        表名1 别名1,表名2 别名2…

        where

        连接成功

select
    e.*,d.*
from
    emp e,dept d;

#如上查询会出现笛卡尔积查询(交叉查询)

#迪卡尔积:两个数据集的集合。

     //因为没有连接条件

#去迪卡尔积查询

等值连接:

    select   e.*,b.*   
    from  emp e,dept d
    where
        e.deptno=d.deptno;   //需要通过两个表中相同的字段进行等值连接

示例1:查询emp表和dept表的所有数据.

        select

        e.*,d.*

        from

        emp e,dept d

        where

        e.deptno = d.deptno;

示例2:查询所有员工的编号,姓名,工资和部门名称以及部门地址。

        select

        e.empno,e.ename,e.sal,d.dname,d.loc

        from

        emp e,dept d

        where

        e.deptno=d.deptno;

示例3:查询部门为10的员工的编号,姓名,工资和部门名称以及部门地址。

        select

        e.empno,e.ename,e.sal,d.dname,d.loc

        from

        emp e,dept d

        where

        e.deptno=d.deptno

        and

         e.deptno=10;

自连接:一张表中的不同字段的连接

示例1:查询员工姓名,工资,领导姓名,领导的工资。

select
e.ename,e.sal,e2.ename,e2.sal
from
emp e,emp e2       #一个表中建立两个对象
where
e.mgr=e2.empno;    #emp表中e对象的mgr字段和e2对象的empno字段进行连接

非等值连接:和

表示 等值字段和非等值的字段进行比较连接输出

示例:查询员工姓名,工资,部门名称,员工的工资等级。

 

select
e.ename,e.sal,d.dname,s.grade 工资等级
from
emp e,dept d,salgrade s
where
e.deptno=d.deptno
and
e.sal between s.losal and s.hisal;    //等值字段和范围字段进行输出

示例2:查询员工姓名,工资,部门名称,员工的工资等级,领导的工资,姓名,工资等级。

select

e.ename,e.sal,d.dname,s.grade 员工工资等级,

m.ename 领导姓名,m.sal 领导工资,sm.grade 领导的工资等级

from

emp e,dept d,salgrade s,emp m,salgrade sm

where

e.deptno=d.deptno

and

e.mgr=m.ename

and

m.sal between sm.losal and sm.hisal;

SQL99:

SQL99是数据查询语句的一种标准,由1999年制定的

  1. 概念:一种多表查询的一种语法
  2. 分类
    1. 内连接
    2. 外连接

1、内连接:就是等值连接(------只有值相等的数据才能被放入结果集

inner join on      #inner关键字可以省略

等值连接

语法:

        select   列名

        from   表名1 别名1 join 表名2 别名2

        on                #判断连接条件

        连接条件

        join              #需要重新加表连接时,作为连接

        表名3 别名3

        on            #重新连接的条件

        连接条件;

示例:查询工资大于1500的员工姓名,工资,部门名称,领导名称

select
    e.ename,e.sal,d.dname,m.ename
from  emp e 
    join dept d   on   e.deptno = d.deptno  #两个表进行连接
    join emp m   on   e.mgr = m.empno          #新加入一个       
where
    e.sal > 1500;

2、外连接

 left:左外连接------以左边的表为主表(主表中的数据会全部显示结果集,从表没有的数据,会显示null)

        始终显示from里左边的表中的所有数据,匹配上右边中的数据则显示匹配上的数据,如果没有可以匹配的,则显示null;

语法:

select   a.name,b.job 

from  t_b b left   

join t_a a

on  b.a_id = a.id;

right:右外连接-----以右边为主表(同上,就是以join右边为主表)

始终显示右边的数据

语法:

select   a.name,   b.job
from  t_b b
right join t_a a
on  b.a_id = a.id;

示例:查询员工姓名及部门名称,部门地址,包括没有人员的部门。

select  e.ename, d.dname, d.loc

from   emp e

right   join dept d

on   e.deptno = d.deptno;

3、分组统计结合聚合函数:

关键字:group by

语法:

        select    列名1

        from   表名

        where

        过滤条件

        group by

        分组1,分组2....

        order by

        排序列1....   limit

示例数据

create table t_group(        
        id int,
        name varchar(20),
        num int
);

create table t_group(
        id int,
        name varchar(20),
        num int
);

insert into t_group values(1,'aa',2);
insert into t_group values(2,'aa',3);
insert into t_group values(3,'bb',4);
insert into t_group values(4,'bb',5);
insert into t_group values(5,'cc',6);
insert into t_group values(6,'dd',7);
insert into t_group values(7,'bb',5);
insert into t_group values(8,'ee',8);
insert into t_group values(9,'cc',6);

示例:将t_group表的数据按照名称分组。

select   *

from    t_group

group by    name;

having:用来过滤分组后的数据

关键字

        where语句在分组之前筛选数据,where语句是在group by 和having之前运行。

而分组后的数据需要使用having关键字来过滤

执行顺序:from-->on(where---join) -->group by -->select--> having -->order by-->limit(分页

 

示例:查询平均工资大于2000元的部门编号和平均工资。

        select  d.dname,avg(e.sal)

        from  emp e, dept d

        where   e.deptno=d.deptno

        group by  d.deptno

        having   avg(e.sal)>2000;

 

示例查询部门人数大于3个的部门的名称,员工的数量。

        select        d.dname,count(e.ename)

        from   emp e

        join dept d

        on   e.deptno=d.deptno

        group by    d.deptno

        having    count(e.ename)>3;

子查询:

        1、概念:将一个查询的结果做另一个查询的条件,就叫做子查询。也叫嵌套查询

        运算时,先得出判断条件,然后再把判断条件嵌入主代码输出

        2、在进行判断条件是进行分段理解,过滤条件不需要分组的使用where,需要分组的使用having

        需要查看的数据则使用select得出,中间需要使用的表写在from里,后面需要求的平均工资就为过滤的条件

运行流程如图:

        1.先查出公司平均薪资

        2.通过from写出需要使用的表

        3.把过滤的条件写到where下面作为要求

        4通过select写出需要查看的列

示例1:查询公司最高工资的人的工资是多少,,姓名是什么?

---公司最高工资是多少?

select max(sal) from emp;   

select  sal, ename

from  emp

where

sal=(select max(sal) from emp);    #查询最高工资

示例2:查询工资比7698工资高的雇员的信息。

select sal from emp where empno=7698       //先得出最高工资的人的工资

select     

* from  emp

where

sal>(select sal from emp where empno=7698);   #在把得出最高工资的人的工资做为条件

子查询:    

  1. 单列子查询
    1. 返回的结果集是单行单列
    2. 结合 运算符使用 > < = !=

  2. 多行子查询

    1. 返回的结果集是多行单列  结合 运算符 in any all

  3. 多列子查询

    1. 返回的结果集是多行多列(可以当成一个结果集)

1、单列子查询

示例1:查询每个部门的编号和最低工资,要求最低工资大于等于部门30的最低工资

--部门30的最低工资是多少?  (先得出最低工资)

select min(sal) from emp where deptno =30;

select   deptno   min(sal)  最低工资                       #最低工资是min(sal)的别名,

from   emp

group by   deptno

having    最低工资 > select min(sal) from emp where deptno =30;

示例2:查询部门的名称,部门员工数量,部门的平均工资,部门的最低收入的员工的姓名。

select ename from emp where sal =min(sal)

select   d.dname 部门编号,  count(e.ename) 部门员工数量,  avg(e.sal)部门平均工资,

(select ename from emp where sal =min(e.sal)) 部门最低工资名字

from   dept d

left join emp e

on   d.deptno=e.deptno

group by   d.deptno;

2、多行子查询

通常使用做一个判断条件使用

  1. 主要使用操作符
    1. in:包含
    2. any :大于任意,小于任意
      1. =any :等同于in 一般使用相对较少
      2.  >any :大于里面的最小值
      3. <any :小于里面的最大值

    3. all:大于最大的,小于最小的

      1. >all:大于里面的最大值

      2. <all:小于里面的最小值

  1. in 示例:查询工资与部门20部门所有员工工资相同的员工信息

---部门20的员工的工资是多少

select sal from emp where deptno=20;

select   *

from   emp

where   sal in(select sal from emp where deptno=20);

1、all关键字

示例:查询工资大于每个部门经理的员工信息

---先查出每个部门经理的工资

select sal from emp where job='manager';

select   *

from   emp e

join (select sal,deptno from emp where job='manager') m

on   e.deptno=m.deptno

group by   e.deptno

having   e.deptno=m.deptno

and   e.sal>m.sal;

2、多列子查询

多列子查询返回的是多行多列,或者单行多列

常用作在结果集上,当成一个表使用在(from中使用

示例:在从事销售工作中出现工资大于1500的员工大于1500的员工的信息

----哪些人从事销售工作?

select * from emp where job = 'salesman'

多表练习题

1、列出至少有4个员工的部门名称

select  d.dname

from   emp e join dept d

on   e.deptno=d.deptno

group by   d.deptno

having

count(e.ename)>4;

2、列出薪金比"SMITH"多的所有员工

select  ename

from   emp

where

sal>(select sal from emp where ename='SMITH');

3、列出所有员工的姓名以及其直接上级的姓名

select   e.ename,m.ename

from   emp e,   emp m

where

e.mgr=m.empno;

4、列出受雇日期早于其直接上级的所有员工的编号、姓名、部门名称

select   e.empno,e.ename,d.dname

from   emp e,  emp m, dept d

where

e.mgr=m.empno

and

e.deptno=d.deptno

and

e.hiredate<m.hiredate;

5、列出所有从事"CLERK"工作的雇员姓名及其部门名称、部门人数

select count(ename) 部门人数,deptno from emp group by deptno;  #先得出每个部门的人数

select

e.ename,d.dname,t.部门人数

from   emp e join dept d

on   e.deptno=d.deptno

join

 (select count(ename) 部门人数,deptno from emp group by deptno) t    #因为部门人数是表格

on    e.deptno=t.deptno

where   job='clerk';

6、列出在部门"SALES"(销售部)工作的员工的姓名,假定不知道销售部的部门编号

select

ename

from

  emp

where

deptno=(select deptno from dept where dname ='sales');

猜你喜欢

转载自blog.csdn.net/weixin_52315708/article/details/131500171