多表查询

连接查询:

  内连接:AB交集

    隐式内连接

    select    t1.name,   t1.age,  t2.dname  from emp t1,  dept  t2  where t1.dep_id=t2.did; --起别名简化书写

    显式内连接

    select * from emp join dept on emp.dep_id=dept_did;  --join和 inner join一个意思

  外连接:

    左外连接:A和AB交集

      查emp所有数据和对应部门信息

      select * from emp left join dept on emp.dep_id=dept.id;    --查emp所有数据

    右外连接:B和AB交集

      select * from emp right join dept on emp.dep_id=dept.id;       --查dept所有数据

 

子查询;单行单列/多行单列/多行多列

  单行单列:

  查比猪八戒工资高的员工信息:

    分两步:①查猪八戒工资

        select salary from emp where name='猪八戒';    -- 查出了3600

        ②查工资高于猪八戒的工资

        select * from emp where salary>3600;

    合二为一:

      select * from emp where salary>(select salary from emp where name='猪八戒');

  多行单列:

     查财务部和市场部所有员工信息

     同理两步走:

      select * from emp where dep_id in( select did from dept where dname='财务部' or dname='市场部');

  多行多列:

    查入职日期是‘2022-2-2’之后,的员工信息和部门信息:

    分两步

      select *from emp where join_date >'2022-2-2';  -- 查出在这之后的员工

      select *from (select *from emp where join_date >'2022-2-2') t1,dept where t1.dep_id=dept.did;     --在第一步查出的表之上查

  

      

posted @   大灰狼21  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示