• DML语句


    DML 操作是指对数据库中表记录的操作,主要包括表记录的插入(insert)、更新(update)、删除(delete)和查询(select),是开发人员日常使用最频繁的操作。

    插入记录

    表创建好后,就可以往里插入记录了,插入记录的基本语法如下

    INSERT INTO tablename (field1, field2, .…, fieldn)VALUES (value1, value2, ..……, valuen);

    向表 emp 中插入:ename 为 zzx1,hiredate 为 2000-01-01,sal 为 2000,deptno 为 1

    mysql> desc emp;
    +----------+---------------+------+-----+---------+-------+
    | Field    | Type          | Null | Key | Default | Extra |
    +----------+---------------+------+-----+---------+-------+
    | ename    | varchar(10)   | YES  |     | NULL    |       |
    | hiredate | date          | YES  |     | NULL    |       |
    | sal      | decimal(10,2) | YES  |     | NULL    |       |
    | deptno   | int(2)        | YES  |     | NULL    |       |
    +----------+---------------+------+-----+---------+-------+
    4 rows in set (0.01 sec)
    
    mysql> insert into emp(ename,hiredate,sal,deptno) values('zzx1','2000-01-01','2000',1);
    Query OK, 1 row affected (0.01 sec)
    

    也可以不用指定字段名称,但是 values 后面的顺序应该和字段的排列顺序一致

    mysql> insert into emp values('lisa','2003-02-01','3000',2);
    Query OK, 1 row affected (0.01 sec)
    
    mysql> insert into emp values('bjguan','2004-04-02','4000',1);
    Query OK, 1 row affected (0.01 sec)
    

    只对表中的 ename 和 sal 字段显式插入值

    mysql> insert into emp(ename,sal) values('dony',1000);
    Query OK, 1 row affected (0.01 sec)
    

    查看实际插入的值

    mysql> select * from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx1   | 2000-01-01 | 2000.00 |      1 |
    | lisa   | 2003-02-01 | 3000.00 |      2 |
    | bjguan | 2004-04-02 | 4000.00 |      1 |
    | dony   | NULL       | 1000.00 |   NULL |
    +--------+------------+---------+--------+
    4 rows in set (0.00 sec)
    

    可以一次性插入多条记录,每条记录之间用逗号分隔

    INSERT INTO tablename (field1, field2, ..…, fieldn)
    VALUES
    (record1_value1, record1_value2, .…, record1_valuen),
    (record2_value1, record2_value2, .…, record2_valuen),
    ...
    (recordn_value1, recordn_value2, .…, recordn_valuen);

    首先创建表 dept

    mysql> create table dept(deptno int(2), deptname varchar(10));
    Query OK, 0 rows affected (0.03 sec)
    
    mysql> desc dept;
    +----------+-------------+------+-----+---------+-------+
    | Field    | Type        | Null | Key | Default | Extra |
    +----------+-------------+------+-----+---------+-------+
    | deptno   | int(2)      | YES  |     | NULL    |       |
    | deptname | varchar(10) | YES  |     | NULL    |       |
    +----------+-------------+------+-----+---------+-------+
    2 rows in set (0.01 sec)
    

    对 dept 表一次插入多条记录

    mysql> insert into dept values(1,'tech'),(2,'sale'),(5,'fin'),(5,'dept5'),(6,'dept6');
    Query OK, 5 rows affected (0.01 sec)
    Records: 5  Duplicates: 0  Warnings: 0
    
    mysql> select * from dept;
    +--------+----------+
    | deptno | deptname |
    +--------+----------+
    |      1 | tech     |
    |      2 | sale     |
    |      5 | fin      |
    |      5 | dept5    |
    |      6 | dept6    |
    +--------+----------+
    5 rows in set (0.00 sec)
    

    更新记录

    UPDATE tablename SET field1=value1, field2=value2, ..., fieldn=valuen [WHERE CONDITION]

    将表 emp 中 ename 为 “lisa” 的薪水(sal)从 3000 改为 4000

    mysql> update emp set sal=4000 where ename='lisa';
    Query OK, 1 row affected (0.01 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> select * from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx1   | 2000-01-01 | 2000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    | bjguan | 2004-04-02 | 4000.00 |      1 |
    | dony   | NULL       | 1000.00 |   NULL |
    +--------+------------+---------+--------+
    4 rows in set (0.00 sec)
    

    update 命令可以同时更新多个表中的数据

    UPDATE t1, t2, ..., tn set t1.field1=expr1, t2.field2=expr2, ..., tn.fieldn=exprn [WHERE CONDITION]

    同时更新表 emp 中的字段 sal 和表 dept 中的字段 deptname

    mysql> select * from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx1   | 2000-01-01 |  100.00 |      1 |
    | lisa   | 2003-02-01 |  200.00 |      2 |
    | bjguan | 2004-04-02 |  100.00 |      1 |
    | dony   | 2005-02-05 | 2000.00 |      4 |
    +--------+------------+---------+--------+
    4 rows in set (0.00 sec)
    
    mysql> select * from dept;
    +--------+----------+
    | deptno | deptname |
    +--------+----------+
    |      1 | tech     |
    |      2 | sale     |
    |      5 | fin      |
    +--------+----------+
    3 rows in set (0.00 sec)
    
    mysql> update emp a, dept b set a.sal=a.sal*b.deptno, b.deptname=a.ename where a.deptno=b.deptno;
    Query OK, 3 rows affected (0.01 sec)
    Rows matched: 5  Changed: 3  Warnings: 0
    
    mysql> select * from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx1   | 2000-01-01 |  100.00 |      1 |
    | lisa   | 2003-02-01 |  400.00 |      2 |
    | bjguan | 2004-04-02 |  100.00 |      1 |
    | dony   | 2005-02-05 | 2000.00 |      4 |
    +--------+------------+---------+--------+
    4 rows in set (0.00 sec)
    
    mysql> select * from dept;
    +--------+----------+
    | deptno | deptname |
    +--------+----------+
    |      1 | zzx1     |
    |      2 | lisa     |
    |      5 | fin      |
    +--------+----------+
    3 rows in set (0.00 sec)
    

    删除记录

    DELETE FROM tablename [where CONDITION]

    将 emp 中 ename 为 “dony” 的记录全部删除

    mysql> delete from emp where ename='dony';
    Query OK, 1 row affected (0.01 sec)
    
    mysql> select * from emp;
    +--------+------------+--------+--------+
    | ename  | hiredate   | sal    | deptno |
    +--------+------------+--------+--------+
    | zzx1   | 2000-01-01 | 100.00 |      1 |
    | lisa   | 2003-02-01 | 400.00 |      2 |
    | bjguan | 2004-04-02 | 100.00 |      1 |
    +--------+------------+--------+--------+
    3 rows in set (0.00 sec)
    

    可以一次删除多个表的数据

    DELETE t1, t2, ..., tn FROM t1, t2, ..., tn [WHERE CONDITION]

    如果 from 后面的表名用别名,则 delete 后面也要用相应的别名,否则会提示语法错误

    同时删除表 emp 和 dept 中 deptno 为 3 的记录

    mysql> select * from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx1   | 2000-01-01 |  100.00 |      1 |
    | lisa   | 2003-02-01 |  200.00 |      2 |
    | bjguan | 2004-04-02 |  100.00 |      1 |
    | bzshen | 2005-04-01 |  300.00 |      3 |
    | dony   | 2005-02-05 | 2000.00 |      4 |
    +--------+------------+---------+--------+
    5 rows in set (0.00 sec)
    
    mysql> select * from dept;
    +--------+----------+
    | deptno | deptname |
    +--------+----------+
    |      1 | tach     |
    |      2 | sale     |
    |      3 | hr       |
    |      5 | fin      |
    +--------+----------+
    4 rows in set (0.00 sec)
    
    mysql> delete a,b from emp a,dept b where a.deptno=b.deptno and a.deptno=3;
    Query OK, 2 rows affected (0.01 sec)
    
    mysql> select * from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx1   | 2000-01-01 |  100.00 |      1 |
    | lisa   | 2003-02-01 |  200.00 |      2 |
    | bjguan | 2004-04-02 |  100.00 |      1 |
    | dony   | 2005-02-05 | 2000.00 |      4 |
    +--------+------------+---------+--------+
    4 rows in set (0.00 sec)
    
    mysql> select * from dept;
    +--------+----------+
    | deptno | deptname |
    +--------+----------+
    |      1 | tach     |
    |      2 | sale     |
    |      5 | fin      |
    +--------+----------+
    3 rows in set (0.00 sec)
    

    查询记录

    SELECT * FROM tablename [where CONDITION]

    显示 emp 表中的全部记录

    mysql> select * from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    +--------+------------+---------+--------+
    3 rows in set (0.00 sec)
    
    mysql> select ename,hiredate,sal,deptno from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    +--------+------------+---------+--------+
    3 rows in set (0.00 sec)
    

    查询不重复的记录

    将表中的记录去掉重复后显示出来,可以用distinct关键字来实现:

    mysql> select ename,hiredate,sal,deptno from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    +--------+------------+---------+--------+
    3 rows in set (0.00 sec)
    
    mysql> select distinct deptno from emp;
    +--------+
    | deptno |
    +--------+
    |      1 |
    |      2 |
    +--------+
    2 rows in set (0.00 sec)
    

    条件查询

    查询所有 deptno 为 1 的记录

    mysql> select * from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    +--------+------------+---------+--------+
    3 rows in set (0.00 sec)
    
    mysql> select * from emp where deptno=1;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    +--------+------------+---------+--------+
    2 rows in set (0.00 sec)
    

    多字段查询

    mysql> select * from emp where deptno=1 and sal<3000;
    +-------+------------+---------+--------+
    | ename | hiredate   | sal     | deptno |
    +-------+------------+---------+--------+
    | zzx   | 2000-01-01 | 2000.00 |      1 |
    +-------+------------+---------+--------+
    1 row in set (0.00 sec)
    

    排序和限制

    SELECT * FROM tablename [WHERE CONDITION] [ORDER BY field1 [DESC | ASC], field2 [DESC | ASC], ..., fieldn [DESC | ASC]]

    DESC:降序排列     ASC:升序排列

    把 emp 表中的记录按照工资高低进行显示

    mysql> select * from emp order by sal;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | dony   | 2005-02-05 | 2000.00 |      4 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    +--------+------------+---------+--------+
    4 rows in set (0.00 sec)
    

    如果排序字段的值一样,则值相同的字段按照第二个排序字段进行排序,依次类推。如果只有一个排序字段,则这些字段相同的记录将会无序排列。

    把 emp 表中的记录按照部门编号 deptno 字段排序

    mysql> select * from emp order by deptno;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    | dony   | 2005-02-05 | 2000.00 |      4 |
    +--------+------------+---------+--------+
    4 rows in set (0.00 sec)
    

    对于 deptno 相同的前两条记录,如果要按照工资由高到低排序,可以使用以下命令

    mysql> select * from emp order by deptno,sal desc;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    | dony   | 2005-02-05 | 2000.00 |      4 |
    +--------+------------+---------+--------+
    4 rows in set (0.00 sec)
    

    对于排序后的记录,如果希望只显示一部分,而不是全部,这时,就可以使用 LIMIT 关键字来实现

    SELECT ... [LIMIT offset_start, row_count]

    其中 offset_start 表示记录的起始偏移量,row_count 表示显示的行数。

    在默认情况下,起始偏移量为 0,只需要写记录行数就可以,这时,实际显示的就是前 n 条记录。例如,显示 emp 表中按照 sal 排序后的前 3 条记录:

    mysql> select * from emp order by sal limit 3;
    +-------+------------+---------+--------+
    | ename | hiredate   | sal     | deptno |
    +-------+------------+---------+--------+
    | zzx   | 2000-01-01 | 2000.00 |      1 |
    | dony  | 2005-02-05 | 2000.00 |      4 |
    | lisa  | 2003-02-01 | 4000.00 |      2 |
    +-------+------------+---------+--------+
    3 rows in set (0.00 sec)
    

    如果要显示 emp 表中按照 sal 排序后从第二条记录开始的 3 条记录,可以使用以下命令:

    mysql> select * from emp order by sal limit 1,3;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | dony   | 2005-02-05 | 2000.00 |      4 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    +--------+------------+---------+--------+
    3 rows in set (0.00 sec)
    

    聚合

    SELECT [field1, field2, ..., fieldn] fun_name
    FROM tablename
    [WHERE where_contition]
    [GROUP BY field1, field2, ..., fieldn
    WITH ROLLUP]]
    [HAVING where_contition]

    参数说明:

    • fun_name 表示要做的聚合操作,也就是聚合函数,常用的有 sum(求和)count(记数)max(最大值)min(最小值)avg(平均值)
    • GROUP BY 关键字表示要进行分类聚合的字段,比如要按照部门分类统计员工数量,部门就应该写在 group by 后面。
    • WITH ROLLUP 是可选语法,表明是否对分类聚合后的结果进行再汇总。
    • HAVING 关键字表示对分类后的结果再进行条件的过滤。

    注意:having 和 where 的区别在于,having 是对聚合后的结果进行条件的过滤,而 where 是在聚合前就对记录进行过滤,如果逻辑允许,尽可能用 where 先过滤记录,这样因为结果集减小,将对聚合的效率大大提高,最后再根据逻辑看是否用 having 进行再过滤。

    在 emp 表中统计公司人数

    mysql> select count(1) from emp;
    +----------+
    | count(1) |
    +----------+
    |        4 |
    +----------+
    1 row in set (0.00 sec)
    

    在此基础上,要统计各个部门的人数

    mysql> select deptno,count(1) from emp group by deptno;
    +--------+----------+
    | deptno | count(1) |
    +--------+----------+
    |      1 |        2 |
    |      2 |        1 |
    |      4 |        1 |
    +--------+----------+
    3 rows in set (0.00 sec)
    

    更详细一些,既要统计部门人数,又要统计总人数

    mysql> select deptno,count(1) from emp group by deptno with rollup;
    +--------+----------+
    | deptno | count(1) |
    +--------+----------+
    |      1 |        2 |
    |      2 |        1 |
    |      4 |        1 |
    |   NULL |        4 |
    +--------+----------+
    4 rows in set (0.00 sec)
    

    统计人数大于 1 的部门

    mysql> select deptno,count(1) from emp group by deptno having count(1)>1;
    +--------+----------+
    | deptno | count(1) |
    +--------+----------+
    |      1 |        2 |
    +--------+----------+
    1 row in set (0.00 sec)
    

    统计公司所有员工薪水总额、最高和最低薪水

    mysql> select * from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    | dony   | 2005-02-05 | 2000.00 |      4 |
    +--------+------------+---------+--------+
    4 rows in set (0.00 sec)
    
    mysql> select sum(sal),max(sal),min(sal) from emp;
    +----------+----------+----------+
    | sum(sal) | max(sal) | min(sal) |
    +----------+----------+----------+
    | 13000.00 |  5000.00 |  2000.00 |
    +----------+----------+----------+
    1 row in set (0.00 sec)
    

    表连接

    当需要同时显示多个表中的字段时,就可以用表连接来实现这样的功能。从大类上分,表连接分为内连接和外连接,它们之间的最主要区别是,内连接仅选出两张表中互相匹配的记录,而外连接会选出其他不匹配的记录。常用的是内连接。

    查询出所有雇员的名字和所在部门名称,因为雇员名称和部门分别存放在表 emp 和 dept 中,因此,需要使用表连接来进行查询

    mysql> select * from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    | dony   | 2005-02-05 | 2000.00 |      4 |
    +--------+------------+---------+--------+
    4 rows in set (0.00 sec)
    
    mysql> select * from dept;
    +--------+----------+
    | deptno | deptname |
    +--------+----------+
    |      1 | tach     |
    |      2 | sale     |
    |      5 | fin      |
    +--------+----------+
    3 rows in set (0.01 sec)
    
    mysql> select ename,deptname from emp,dept where emp.deptno=dept.deptno;
    +--------+----------+
    | ename  | deptname |
    +--------+----------+
    | zzx    | tach     |
    | bjguan | tach     |
    | lisa   | sale     |
    +--------+----------+
    3 rows in set (0.00 sec)
    

    外连接又分为左连接右连接

    • 左连接:包含所有的左边表中的记录甚至是右边表中没有和它匹配的记录。
    • 右连接:包含所有的右边表中的记录甚至是左边表中没有和它匹配的记录。

    查询 emp 中所有用户名和所在部门名称

    mysql> select * from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    | dony   | 2005-02-05 | 2000.00 |      4 |
    +--------+------------+---------+--------+
    4 rows in set (0.00 sec)
    
    mysql> select * from dept;
    +--------+----------+
    | deptno | deptname |
    +--------+----------+
    |      1 | tach     |
    |      2 | sale     |
    |      5 | fin      |
    +--------+----------+
    3 rows in set (0.00 sec)
    
    mysql> select ename,deptname from emp left join dept on emp.deptno=dept.deptno;
    +--------+----------+
    | ename  | deptname |
    +--------+----------+
    | zzx    | tach     |
    | bjguan | tach     |
    | lisa   | sale     |
    | dony   | NULL     |
    +--------+----------+
    4 rows in set (0.00 sec)
    

    右连接和左连接类似,两者之间可以互相转化,上面的例子可以改写为如下的右连接

    mysql> select ename,deptname from dept right join emp on dept.deptno=emp.deptno;
    +--------+----------+
    | ename  | deptname |
    +--------+----------+
    | zzx    | tach     |
    | bjguan | tach     |
    | lisa   | sale     |
    | dony   | NULL     |
    +--------+----------+
    4 rows in set (0.00 sec)
    

    子查询

    某些情况下,当进行查询的时候,需要的条件是另外一个 select 语句的结果,这个时候,就要用到子查询。用于子查询的关键字主要包括 innot in=!=existsnot exists 等。

    从 emp 表中查询出所有部门在 dept 表中的所有记录

    mysql> select * from emp
        -> where deptno
        -> in(select deptno from dept);
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    +--------+------------+---------+--------+
    3 rows in set (0.00 sec)
    

    如果子查询记录数唯一,还可以用 = 代替 in

    mysql> select * from emp
        -> where deptno = (select deptno from dept);
    ERROR 1242 (21000): Subquery returns more than 1 row
    mysql> select * from emp
        -> where deptno = (select deptno from dept limit 1);
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    +--------+------------+---------+--------+
    2 rows in set (0.00 sec)
    

    某些情况下,子查询可以转化为表连接

    mysql> select * from emp where deptno in(select deptno from dept);
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    +--------+------------+---------+--------+
    3 rows in set (0.00 sec)
    
    mysql> select emp.* from emp, dept where emp.deptno=dept.deptno;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    +--------+------------+---------+--------+
    3 rows in set (0.00 sec)
    

    注意:子查询和表连接之间的转换主要应用在两个方面。

    • MySQL 4.1 以前的版本不支持子查询,需要用表连接来实现子查询的功能.
    • 表连接在很多情况下用于优化子查询。

    记录联合

    将两个表的数据按照一定的查询条件查询出来后,将结果合并到一起显示出来,这个时候,就需要用 unionunion all 关键字来实现这样的功能

    SELECT * FROM t1
    UNION | UNION ALL
    SELECT * FROM t2
    ...
    UNION | UNION ALL
    SELECT * FROM tn;

    UNION 和 UNION ALL 的主要区别是 UNION ALL 是把结果集直接合并在一起,而 UNION 是将 UNION ALL 后的结果进行一次 DISTINCT,去除重复记录后的结果。

    将 emp 和 dept 表中的部门编号的集合显示出来

    mysql> select * from emp;
    +--------+------------+---------+--------+
    | ename  | hiredate   | sal     | deptno |
    +--------+------------+---------+--------+
    | zzx    | 2000-01-01 | 2000.00 |      1 |
    | lisa   | 2003-02-01 | 4000.00 |      2 |
    | bjguan | 2004-04-02 | 5000.00 |      1 |
    | dony   | 2005-02-05 | 2000.00 |      4 |
    +--------+------------+---------+--------+
    4 rows in set (0.00 sec)
    
    mysql> select * from dept;
    +--------+----------+
    | deptno | deptname |
    +--------+----------+
    |      1 | tach     |
    |      2 | sale     |
    |      5 | fin      |
    +--------+----------+
    3 rows in set (0.00 sec)
    
    mysql> select deptno from emp
        -> union all
        -> select deptno from dept;
    +--------+
    | deptno |
    +--------+
    |      1 |
    |      2 |
    |      1 |
    |      4 |
    |      1 |
    |      2 |
    |      5 |
    +--------+
    7 rows in set (0.00 sec)
    

    将结果去掉重复记录后显示如下

    mysql> select deptno from emp
        -> union
        -> select deptno from dept;
    +--------+
    | deptno |
    +--------+
    |      1 |
    |      2 |
    |      4 |
    |      5 |
    +--------+
    4 rows in set (0.00 sec)
    
  • 相关阅读:
    Django视图
    Django路由系统
    Django概述,配置文件,web框架本质,Django框架
    Mysql
    R语言之常用函数
    从PCA、PLS-DA、OPLS-DA学习线性代数和矩阵
    算法第一关
    别人处理二代测序的流程
    reportlab包使用指南
    Python 文本(txt) 转换成 EXCEL(xls)
  • 原文地址:https://www.cnblogs.com/qiuxirufeng/p/10802396.html
Copyright © 2020-2023  润新知