博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql 入门经典(第五版) Ryan Stephens 学习笔记  第四部分:建立复杂的数据库查询/...
阅读量:4322 次
发布时间:2019-06-06

本文共 1687 字,大约阅读时间需要 5 分钟。

第十三章: 在查询表里结合表

 1、等值结合 :  

// 选择 tabla_a 和table_b 中id相等的行,输出 他们的id 和name

  select table_a.id , table_a.name , table_b.id , table_b.name from table_a , table_b where table_a.id = table_b.id ;

  或者使用 inner join 

  select table_a.id , table_a.name , table_b.id ,table_b.name from table_a inner join table_b on table_a.id = table_b.id;

  或者使用别名

  select a.id , a.name , b.id ,b.name from table_a  a ,table_b  b where a.id = b.id and a.salary > 2000;

2、不等值结合

  与等值结合语法相同,只是将 等号改为 不等号但是不等值结合会输出多余的信息,因为 table A中每行数据都要和 table B中每一行数据进行比较 。

若a.id != b.id ,则输出 相应的信息。

 

3、外部结合

4、自结合

  select a.last_name , a.first_name ,b.first_name , b.last_name from table_a  a,table_a b where a.last_name = b.last_name ;  

应用:  在一个保存了 雇员标示号码,姓名,雇员主管标示号码的表里。

  select * from emp;

        id     name  mgr_id

        1  john      0

        2   mary    1

        3   steve       1

        4   jack     2

        5   sue    2

  现在需要找到 雇员与 雇主的关系

  select  e1.name , e2.name from emp e1,emp e2 where e1.mgr_id = e2.id ;

    name   name

    mary  john

    steve  john

    jack   mary

    sue   mary

 inner join / left join / right join 可以参考 

   也可以参考 http://www.cnblogs.com/xxlhcjh/p/4309982.html

第十四章: 使用子查询定义未确定的数据

1、 子查询与select 结合使用

  select colum from table where column where  columnb=(select colum_name from ...);

2、   子查询与inset结合使用

  insert table1 select column1 from table2 where cloumn2>(select ......);

  将表二中满足条件的某几项 插入到表1 中,注: 插入的项数 = table1的column数

3、 子查询与 update 结合使用

4、 子查询与delete 结合使用

 

第十五章: 组合多个查询

1、 union 

  select id from stu union select id from jobe ;        // 当两个table中id相同时,不重复输出。

2、 union all

  select id from stu union all select id from jobe ;    // 当两个table中id相同时,重复输出。

转载于:https://www.cnblogs.com/NeilZhang/p/5561018.html

你可能感兴趣的文章
Ajax制作智能提示搜索
查看>>
打赏页面
查看>>
JAVA之线程同步的三种方法
查看>>
OOP之属性继承和方法继承
查看>>
PostgreSQL调用函数
查看>>
ASP.NET MVC+EF框架+EasyUI实现权限管理(附源码)
查看>>
sitecore系统教程之体验编辑器中创建一个项目
查看>>
socket笔记
查看>>
Java 概述及安装使用
查看>>
helloworld
查看>>
港交所OMD-C对接笔记
查看>>
线程安全问题了解一下
查看>>
转:IPv4的地址真的用光了吗
查看>>
java rmi远程方法调用实例
查看>>
Linux设置环境变量小结
查看>>
syslog()用法
查看>>
Java 内存区域和GC机制
查看>>
STM32上不使用外部晶振,OSC_IN和OSC_OUT的接法
查看>>
设计模式六大原则
查看>>
android中的数据库操作
查看>>