java学习之mysql第二十一天( --外键-- 链接查询--嵌套查询--)

你想要成功吗?你愿意为这份成功付出什么?

查询代码的书写顺序和执行顺序
查询语句书写顺序:select – from- where- group by- having- order by-limit
查询语句执行顺序:from - where -group by - having - select - order by-limit
多表查询
多表查询有如下几种:
合并结果集;UNION 、  UNION ALL
连接查询
内连接  [INNER] JOIN  ON 
外连接  OUTER JOIN ON
左外连接 LEFT [OUTER] JOIN
右外连接 RIGHT [OUTER] JOIN
自然连接  NATURAL JOIN
子查询


 1. 创建外键的第二种方式 ----外键让没有关系的表产生练习;

          主键:   create table student (sid int primary key ,sname varchar(10));

         从键:   create table score ( sid int, score int ) ;

          添加外键:  alter table score add constraint  外键名 foreign key( sid  ) references student (sid);

    alter 更新表结构 删除(  要使用约束的别名来进行删除, 外键名不能重复 )

         删除外键:  alter table score foreign key drop 外键名;

   2. 表与表之间的关系  一对多  多对多  多对一;

         多对多, 利用第三张表来作为从表, 拥有其他两个主表的外键;

扫描二维码关注公众号,回复: 1742315 查看本文章

  3 . 合并查询 : union 代表取两个表的并集( 字段名和类型名相同)  unionall 把两个表的数据合并到一起

              select * from A union  select from B;    // 合并A和B 表;

  4. 多表查询: 这样查询会产生 笛卡尔积;

              select * from A ,B ;

  5. 99查询法去除重复的, 学生表和分数表一起查询, 俩表里面有个相同的字段  stuid;

            select from student , score where student.stuid = score.stuid ;

   6. 从两个表中 查询到学生的 编号和分数;

         select s.stuid , c.score from  student s , score c where  c.stuid = s.stuid;

2. 链接查询:

        1. 内链接   inner join  链接的表  on 后面跟条件;

                select * from student s inner join score c on s.stuid = c.stuid ;

        2. 外链接:  分为左外链接 和 右外链接; 

                左外链接以左边的表为主, 会查询出左边表的所有数据; left outer join ,

                右外键链接以右边为主, 会查询出右边所有的数据;  right outer join;

             select *  from student left join score on student.stuid = score.stuid ;

             select *  from student right join score on student.stuid = score.stuid ;

       3.自然链接 natural join : 

              自动匹配两个表中相同字段的值; 要求字段名和类型一样;

             select * from student natural join score;

4. 子查询( 嵌套查询  )


  

1. 查询 工资高于jones的员工信息;

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

2. 查询与Scott同一个部门的员工; 

   select  *  from emp where deptho= (select deptno from emp where ename = 'scott');

3. 查询工资高于30部门最高工资的所有信息;

 select *  from emp where sal>( select Max( sal ) from emp where deptno=30  );

4.查询工作和工资都和martin 完全相同的员工的信息;

       select * from emp where sal=(select sal  from emp where ename ='MARTIN') and 

        job =(select job  from emp where ename ='MARTIN');

  方式二: in可以找到多个值;

       select * from emp where ( sal, job  ) in ( select sal ,job from emp where ename = 'martin' );

 5. 有两个直接下属的员工的信息;

       select * from  where empno in ( select mgr from group by mgr having count(mgr)>2); 

 6. 查询编号为7788 员工名称 所有信息;

      select * from  emp e , dept d where e.depthno= d.depthno and e.empno=7788;

7.求各个部门薪水最高的员工的所有信息;  将查询的结果当成一张表;

select * from emp e1, (select max(sal) msal , deptno from emp GROUP BY deptno) e2 
where e1.sal=e2.msal and e1.deptno=e2.deptno;

注意: 一旦查两种表必须去除重复积 也就是让他们相同的属性相等;


猜你喜欢

转载自blog.csdn.net/a18755425397/article/details/80684328