zoukankan      html  css  js  c++  java
  • 多表查询

    先准备两张表

    一张员工表 一张部门表

         

     要交叉连接, 生成笛卡尔积, 在在笛卡尔积的基础上建立连接查询数据

    (有内连接查询, 左链接查询, 右链接查询, 还有全链接查询)

     1:交叉连接:不适用任何匹配条件, 生成笛卡尔积

    select * from emp, dep

    2:内连接 把两张表有对应关系的记录链接成一张虚拟表

    用emp,和dep 表来举例

    语法:select  * from emp inner join dep on emp.dep_id = dep.id;

    应用

    select * from emp,dep where emp.dep_id =dep.id and dep.name='技术';   # 不要用where 做连表的活,所以应该内连接语法来写

    select * from emp inner join dep on emp.dep_id =dep.id where dep.name='技术';

     

    3:左链接  在内连接的基础上保留左边没有对应的关系的记录

    select  * from emp left join dep on emp.dep_id = dep.id:

    4:右链接,在内连接的基础上保留右边没有对应的关系的记录

    select * from emp right join dep on emp.dep_id = dep.id;

    5 全链接:在内连接的基础上,保留左右边没有对应关系的记录

    select  * from emp left join dep on emp.dep_id = dep.id

    unique

    select * from emp right join dep on emp.dep_id = dep.id;

     需要补充的是多表连接可以不断的与虚拟表连接:

    用单表中的列子举例说明:

    现要查询各个部门员工的工资最高的人的信息 需求

    select  t1.* from emp as t1  #把emp 另赋别名t1, 方便操作, 查询t1 的信息  相当于 select * from t1

     inner join                   #内连接

    (select post,max(salary) as  ms from emp  group by post )as t2   # 内连接一个虚拟表

    on t1.post=p2.post  where t1.salary =t2.ms;

     

  • 相关阅读:
    springboot对JPA的支持
    Hibernate-什么是orm思想
    利用Struts拦截器完成文件上传功能
    Struts2的CRUD
    struts2的初步认识
    Maven搭建
    java虚拟机
    Map集合
    Set集合(TreeSet)
    Set集合的
  • 原文地址:https://www.cnblogs.com/lx3822/p/9021599.html
Copyright © 2011-2022 走看看