• MySQL 数据库 定义参数【连接查询】


     

    目录

    内连接查询(inner  join)

    左连接查询 left join

    右连接 right join

    全连接、合并查询 union


    内连接查询(inner  join

    关键字:inner  join   on

    语句:select * from 表名 inner join 表名 on 条件 [ where/group by/order by 等条件表达式 ];

    说明:组合两个表中的记录,返回关联字段相符的记录,也就是返回两个表的交集(阴影)部分。

    在比较时 若两张表字段名有相同的 则需要限定表名(格式:表名.字段)

    -- 表名.*  表示这张表的所有字段  表名.ename  表示这张表的ename字段

    查询employees表的姓名、部门编号和departments表的部门编号,且部门编号相对应

    SELECT employees.ename,employees.deptno,departments.*

    FROM employees,departments

    WHERE employees.deptno=departments.dno;

    左连接查询 left join

    关键字:left join on / left outer join on

    语句:SELECT  * FROM a_table a left join b_table b ON a.a_id = b.b_id;

    说明: left join 是left outer join的简写,它的全称是左外连接,是外连接中的一种。 左(外)连接,左表(a_table)的记录将会全部表示出来,而右表(b_table)只

    会显示符合搜索条件的记录。右表记录不足的地方均为NULL。

    右连接 right join

    关键字:right join on / right outer join on

    语句:SELECT  * FROM a_table a right outer join b_table b on a.a_id = b.b_id;

    说明:right join是right outer join的简写,它的全称是右外连接,是外连接中的一种。与左(外)连接相反,右(外)连接,左表(a_table)只会显示符合搜索条件的

    记录,而右表(b_table)的记录将会全部表示出来。左表记录不足的地方均为NULL。

    全连接、合并查询 union

    关键字:union /union all

    语句:(select * from 表名 ) union (select * from 表名2 )

             (select colum1,colum2...columN from tableA ) union all (select colum1,colum2...columN from tableB )

    union语句注意事项:

             1.通过union连接的SQL它们分别单独取出的列数必须相同;

             2.不要求合并的表列名称相同时,以第一个sql 表列名为准;

             3.使用union 时,完全相等的行,将会被合并,由于合并比较耗时,一般不直接使用 union 进行合并,而是通常采用union all 进行合并;

             4.被union 连接的sql 子句,单个子句中不用写order by ,因为不会有排序的效果。但可以对最终的结果集进行排序;

               (select id,name from A order by id) union all (select id,name from B order by id); //没有排序效果

               (select id,name from A ) union all (select id,name from B ) order by id; //有排序效果

    采用union语句连接,会自动去重union all保留那些重复的数据;

     

  • 相关阅读:
    第十章 二叉树的各种遍历
    ArrayList和linkedList的区别精简概述
    C专家编程 第11章 你懂得C,所以C++不再话下 11.1 初识OOP
    栈的计算(入栈出栈顺序是否合法)-代码
    重装系统后如何恢复以前的文件?详细教程大揭秘!
    Linux网络编程04
    vue03
    人才流失不断,苹果 M1 芯片首席设计师重回老东家——英特尔
    Docker、Jenkins、Git 自动化部署 SpringBoot 项目(从零到搭建完成)
    Go 1.19 发行说明(翻译)
  • 原文地址:https://blog.csdn.net/weixin_57099902/article/details/128191500