SQl 语句 表的连接

当涉及到多个表查询时就需要使用将表连接的方法进行查询。

SQL语句连接的方式根本上分为5种:

1 ?EQUI JOIN
2 ?SEMI JOIN
3 ?ANTI JOIN
4 ?CROSS JOIN
5 ?DIVISION

1.EQUI JOIN

这是最基本的JOIN(连接)操作包括:内连接,左连接,右连接,全连接

内连接:内连接使用比较运算符根据每个表共有的列的值匹配两个表中的行。

 语法格式:inner join

1 select  *
2 from  Student_One
3 inner join   Student_Two
4 on  Student_One.sno=Student_One.sno;

左连接:左连接的结果集包括left join 子句中指定的所有的行,不仅仅连接所匹配的行,如果左表的某行在右表中没有匹配行,在相关联的结果集行中右表的所有选择列表为空值。

  语法格式:left join

1 select  *
2 from  Student_One
3 left join   Student_Two
4 on  Student_One.sno=Student_One.sno;

右连接:右连接是左连接的反向连接。返回右表的所有行,如果右表某行在左表没有匹配行,则左表对应的返回空值。

  语法格式:right join

1 select *
2 from  Student_One
3 right join   Student_Two
4 on  Student_One.sno=Student_One.sno;

全连接:返回左表和有右表中的所有行(有匹配的返回匹配的行)。当某行在另一个表中没有匹配时,另一个表的选择列表列包含空值。如果有匹配行,则整个结果集行包含基表的数据值。

  语法格式:full join

1 select *
2 from  Student_One
3 full join   Student_Two
4 on  Student_One.sno=Student_One.sno;

交叉连接:交叉连接返回左表中的所有行,左表的每一行与右表所有行组合。交叉连接也叫笛卡儿积。

注意:交叉连接有两种,显式和隐式的,没有on子句,返回的是两表的笛卡尔积。

隐式:

1 select *
2 from  Student_One,Student_Two

显式:

1 select *
2 from Student_One
3 cross join Student_Two;

from子句中的表或者视图可通过内连接或全连接按任意顺序指定;但是用左连接或右连接指定表或视图的顺序很重要。

2.SEMI JOIN

这种连接关系在SQL中有两种表现方式:使用IN 或者 使用EXITS。

IN 比 EXITS 的可读性好

EXITS 比 IN 的表达性好(适合复杂的语句)

例子:

1 -- Using IN
2 select *
3 FROM author
4 where author.id IN (select book.author_id from book)
5
6 -- Using EXISTS
7 select *
8 from author
9 where EXISTS (select 1 from book where book.author_id = author.id)

3.ANTI JOIN

这种连接的关系和SEMI JOIN 相反。在 IN 或 EXITS 前加 NOT 关键字

1 -- Using IN
2 select *
3 from author
4 where  author.id NOT IN (select book.author_id from book)
5
6 -- Using EXISTS
7 select *
8 from  author
9 where NOT EXISTS (select 1 from book where book.author_id = author.id)

多表连接时SQl的常用技术 必须掌握!

时间: 2025-01-07 03:47:53

SQl 语句 表的连接的相关文章

Mybatis中如何在SQL语句表名中使用参数

insert into prefix_${table_name} (a, b, c) values (#{a}, #{b}, #{c}) ${} 表示直接使用字面量(literal value) #{} 表示这个是个参数 如果 table_name 是 "ABC" 则 ${table_name} 是 ABC #{table_name} 是 "ABC" Mybatis中如何在SQL语句表名中使用参数,布布扣,bubuko.com

优化子查询sql语句为内连接

背景: 希望提高查询的效率,从sql语句中频繁出现的子查询入手. 数据表如下:Student表中的CityCode对应于City表中的Code. Student表:                      City表: 子查询方式: sql语句如下: 1 select * from Student 2 where CityCode 3 in 4 (select Code from City) 内连接方式:sql语句如下: 1 select a.* from 2 Student a inner

SQL语句表操作

创建表create table student(    id int not null auto_increment primary key comment '这是主键',    sno varchar(10) comment '这是姓名',    sex int default 1 comment '这是性别 1代表男 0代表女',    brithday date);数据类型: int date datetime  timestemp  varchar(20)查看表结构desc studen

Sql语句 表中相同的记录(某个字段)只显示一条,按照时间排序显示最大或最小

原始表数据: 想要的结果数据为: sql语句: select * from Table_1 where DT in(select min(DT)   from Table_1 group by AccountID)  --DT为时间字段

sql 语句的各种连接

数据表: 1.两种连接表现方式一样 其中 where 条件语句可以省略,当时join 的on 语句不可省略 2.左连接,右连接 左连接:返回左表的所有数据,并根据条件返回左右表的连接结果,如果未匹配到值则显示null. 右连接:与左连接相反. 另外:左右连接有一定的排序功能,比如左连接则根据左表的数据,以左表的每一条数据对应右表的所有数据,依次类推. select * from T_student s,T_class c 默认同 select * from  T_student s right

【测试】使用hr用户下的employees和departments表写一条SQL语句,(MG连接)

SQL> select * from employees d, departments t where d.department_id=t.department_id; 106 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 1343509718 --------------------------------------------

sql语句中的连接方式

两张表 部门表(dept)和员工表(usert)中所有的数据 1.--左连接 usert(员工表) dept(部门表)--显示员工信息时,有的员工可能还没有分配到某个具体的部门(新进员工),其所属部门一项就没有数据,但是员工仍然需要显示,即员工表数据需要全部显示select u.name,d.deptname  from usert u left join dept d on u.deptid=d.deptid order by u.id 2.--右连接--新部门还没有员工,显示时也需要把部门显

zbb20170816 oracle Oracle 查看表空间、数据文件的大小及使用情况sql语句

oracle Oracle 查看表空间.数据文件的大小及使用情况sql语句 --表空间 --1G=1024MB --1M=1024KB --1K=1024Bytes --1M=11048576Bytes --1G=1024*11048576Bytes=11313741824Bytes SELECT a.tablespace_name "表空间名", total "表空间大小", free "表空间剩余大小", (total - free) &qu

让你提前认识软件开发(20):如何在C语言里面执行SQL语句?

[文章摘要] 在通信类软件中,程序经常需要与数据库打交道.为了实现诸如从数据库中获取数据.更新数据库表某字段.插入或删除某条数据等功能,就需要在C语言程序中构造一些SQL语句,并用函数来执行这些SQL语句. 本文介绍如何在C语言程序中构造并执行SQL语句,为相关软件开发工作的开展提供了参考. [关键词] SQL语句  C语言  程序  流程  开发 一.为什么要在C语言程序中执行SQL语句? 在C语言程序中执行SQL语句的原因有以下几个: (1) 程序需要获取数据库中某数据表的字段值,并对这些字