`

inner join left join right join on where

 
阅读更多

理解 inner join 和 outer join
inner join :从两个或多个表中,仅返回满足关联条件的行。

left (outer) join :tableA left join tableB 返回表A中所有的记录,对表B中没有符合关联条件的记录返回NULL

full (outer) join:tableA full join tableB 返回表A和B中所有的记录,没有符合关联条件的记录返回NULL。


TABLEA:

C_ID         C_ZT       C_NAME
1            关闭        工单1
2            正常        工单2
3            关闭        工单3
4            正常        工单4


注意:表B中第三条记录的C_NAME为"工单5" 不是"工单3"
TABLEB:

C_ID         C_ZT       C_NAME
1            关闭        工单1
2            正常        工单2
3            关闭        工单5
4            正常        工单4


=================================================

Sql代码 复制代码 收藏代码
  1. select a.*,b.* from tablea a inner join tableb b on (a.c_id=b.c_id and a.c_name = b.c_name)  
select a.*,b.* from tablea a inner join tableb b on (a.c_id=b.c_id and a.c_name = b.c_name)


rownum C_ID   C_ZT    C_NAME         C_ID   C_ZT    C_NAME
1 1 关闭 工单1 1 关闭 工单1
2 2 正常 工单2 2 正常 工单2
3 4 正常 工单4 4 正常 工单4


Sql代码 复制代码 收藏代码
  1. select a.*,b.* from tablea a left join tableb b on (a.c_id=b.c_id and a.c_name = b.c_name)  
select a.*,b.* from tablea a left join tableb b on (a.c_id=b.c_id and a.c_name = b.c_name)


rownum C_ID   C_ZT    C_NAME         C_ID   C_ZT    C_NAME
1 1 关闭 工单1 1 关闭 工单1
2 2 正常 工单2 2 正常 工单2
3 4 正常 工单4 4 正常 工单4
4 3 关闭 工单3 - - -
Sql代码 复制代码 收藏代码
  1. select a.*,b.* from tablea a right join tableb b on (a.c_id=b.c_id and a.c_name = b.c_name)  
select a.*,b.* from tablea a right join tableb b on (a.c_id=b.c_id and a.c_name = b.c_name)


id C_ID   C_ZT      C_NAME        C_ID   C_ZT      C_NAME
1 1 关闭 工单1 1 关闭 工单1
2 2 正常 工单2 2 正常 工单2
3 -     -         3 关闭 工单5
4 4 正常 工单4 4 正常 工单4


Sql代码 复制代码 收藏代码
  1. select a.*,b.*  from tablea a full join tableb b on (a.c_id=b.c_id and a.c_name = b.c_name)  
select a.*,b.*  from tablea a full join tableb b on (a.c_id=b.c_id and a.c_name = b.c_name)


返回五条记录:
第四条是别名为A的记录,由于在别名为B的表中没有找到记录所以B中的字段值返回了空
第四条是别名为B的记录,由于在别名为A的表中没有找到记录所以A中的字段值返回了空

rownum C_ID   C_ZT    C_NAME         C_ID   C_ZT    C_NAME
1 1 关闭 工单1 1 关闭 工单1
2 2 正常 工单2 2 正常 工单2
3 4 正常 工单4 4 正常 工单4
4 3 关闭 工单3 - - -
5 - - - 3 关闭 工单5




如果查询语句为:

Sql代码 复制代码 收藏代码
  1. select gd.*,gd2.* from tableA  gd full join tableB  gd2 on (gd.c_id = gd2.c_id and gd.c_name = '工单5');  
select gd.*,gd2.* from tableA  gd full join tableB  gd2 on (gd.c_id = gd2.c_id and gd.c_name = '工单5');


输出结果为:

rownum C_ID   C_ZT    C_NAME         C_ID   C_ZT    C_NAME
1 1 关闭 工单1 -     - -
2 2 正常 工单2 -     - -
3 3 关闭 工单3 -     - -
4 4 正常 工单4 -     - -
5 -    -   -   1 关闭 工单1  
6 -    -   -   2 正常 工单2
7 -   -    -   3 关闭 工单5
8 -   -    -   4 正常 工单4


注意是8行。

Sql代码 复制代码 收藏代码
  1. select gd.*,gd2.* from tableA  gd full join tableB  gd2 on (gd.c_id = gd2.c_id and gd2.c_name = '工单5');  
select gd.*,gd2.* from tableA  gd full join tableB  gd2 on (gd.c_id = gd2.c_id and gd2.c_name = '工单5');

 

rownum C_ID   C_ZT    C_NAME         C_ID   C_ZT    C_NAME
1 3 关闭 工单3 3 关闭 工单5
2 1 关闭 工单1 - - -
3 4 正常 工单4 - - -
4 2 正常 工单2 - - -
5 - - - 1 关闭 工单1
6 - - - 2 正常 工单2
7 - - - 4 正常 工单4



注意是7行。

再看下面的:

Sql代码 复制代码 收藏代码
  1. select gd.*,gd2.* from tableA  gd left join tableB  gd2 on (gd.c_id = gd2.c_id and gd.c_name = '工单5');  
select gd.*,gd2.* from tableA  gd left join tableB  gd2 on (gd.c_id = gd2.c_id and gd.c_name = '工单5');


rownum C_ID   C_ZT    C_NAME         C_ID   C_ZT    C_NAME
1 1 关闭 工单1 - - -
2 2 正常 工单2 - - -
3 3 关闭 工单3 - - -
4 4 正常 工单4 - - -


Sql代码 复制代码 收藏代码
  1. select gd.*,gd2.* from tableA  gd left join tableB  gd2 on (gd.c_id = gd2.c_id and gd2.c_name = '工单5');  
select gd.*,gd2.* from tableA  gd left join tableB  gd2 on (gd.c_id = gd2.c_id and gd2.c_name = '工单5');

 

rownum C_ID   C_ZT    C_NAME         C_ID   C_ZT    C_NAME
1 3 关闭 工单3 3 关闭 工单5
2 1 关闭 工单1 - -   -     
3 4 正常 工单4 - - -
4 2 正常 工单2           -       - -


Sql代码 复制代码 收藏代码
  1. select gd.*,gd2.* from tableA  gd left join tableB  gd2 on (gd.c_id = gd2.c_id and gd.c_name = '工单2');  
select gd.*,gd2.* from tableA  gd left join tableB  gd2 on (gd.c_id = gd2.c_id and gd.c_name = '工单2');

 

rownum C_ID   C_ZT    C_NAME         C_ID   C_ZT    C_NAME
1 1 关闭 工单1 - - -
2 2 正常 工单2 2 正常 工单2
3 3 关闭 工单3 - - -
4 4 正常 工单4           -        - -




以前我一值会记录on中的条件和where中一样,会对最终的结果做过滤,实际上是理解错了。
比如A left join B 无论在ON 子句中加什么条件去过滤A表中的记录是不可能的,都会输出A表中所有的记录,唯一有区别的时,如果在做两个表关联的记录时,如果找不到对应的关联记录就对B表中的字符赋予NULL。

效果等同于,先对两个条进行内联接,然后都对A表中没有匹配上的记录全部输出,对应B中的字段赋予NULL。

where 子句中的条件是会对联接后的记录做过滤的。比如:

Sql代码 复制代码 收藏代码
  1. select gd.*,gd2.* from tableA gd left join tableB gd2 on (gd.c_id = gd2.c_id ) where gd.c_name = '工单2';  
select gd.*,gd2.* from tableA gd left join tableB gd2 on (gd.c_id = gd2.c_id ) where gd.c_name = '工单2';



只会查询到一条记录,并不是四条记录

rownum C_ID   C_ZT    C_NAME         C_ID   C_ZT    C_NAME
1 2 正常 工单2 2 正常 工单2

 

分享到:
评论

相关推荐

    join on 语句及扩展

    Join on/inner join on/full join on/full outer join on/left join on/right join on/cross join on; 在使用jion时,on和where条件的区别;

    pcw0118#ZXBlog#七种inner join1

    第一种inner join: 第二种left join: 第三种right join: 第四种left join where b.id is null: 第五种

    解析sql语句中left_join、inner_join中的on与where的区别

    以下是对在sql语句中left_join、inner_join中的on与where的区别进行了详细的分析介绍,需要的朋友可以参考下

    K3触发器实现对物料单据管控 .txt

    left join seorder as t4 on t4.finterid=t3.forderinterid where isnull(t4.fbillno,'no')<>'no' and t2.finterid=@finterid240 return end --控制委外加工生产任务单领料日期不对小于计划开工日期 if (@...

    LINQ to SQL语句之Join和Order By

    [t0].[Discontinued] FROM [dbo].[Products] AS [t0] LEFT OUTER JOIN [dbo].[Suppliers] AS [t1] ON [t1].[SupplierID] = [t0].[SupplierID] WHERE ([t1].[Country] = @p0) AND ([t0].[UnitsInStock] = @p1) -- @p0...

    MySQL数据库:USING子句.pptx

    表名1 LEFT| RIGHT JOIN 表名2 USING (列名) [WHERE 条件表达式] ; 【例】 查找Members表中所有订购过图书的会员的姓名。 SELECT Distinct 姓名 FROM Members JOIN Sell USING (用户号); 与下列语句等价: SELECT ...

    SQL 语法 SQL 总结 SQL教程

    SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL Union SQL Select Into SQL Create DB SQL Create Table SQL Constraints SQL Not Null SQL Unique SQL Primary Key SQL Foreign Key ...

    MySQL中join语句的基本使用教程及其字段对性能的影响

    … FROM table1 INNER|LEFT|RIGHT JOIN table2 ON conditiona table1 通常称为左表,table2 称为右表。ON 关键字用于设定匹配条件,用于限定在结果集合中想要哪些行。如果需要指定其他条件,后面可以加上 WHERE 条件...

    MySQL中基本的多表连接查询教程

    一、多表连接类型 1. 笛卡尔积(交叉连接) ...一般使用LEFT [OUTER] JOIN或者RIGHT [OUTER] JOIN  2. 内连接INNER JOIN 在MySQL中把I SELECT * FROM table1 CROSS JOIN table2 SELECT * FROM table1 JOIN table2 SELE

    ACCESS语法

    LEFT及RIGHT JOIN SELECT 客户.公司名称, 客户.连络人姓名, 订单.客户编号 FROM 客户 LEFT JOIN 订单 ON 客户.客户编号 = 订单.客户编号 WHERE (订单.客户编号 Is Null) 11-2 查询指令 四种动作查询指令

    你的sql该优化了

    1.查询SQL尽量不要使用select * ,而是select具体字段 2.如果知道查询结果只有一条,或者只要最大/最小一...9.Inner join、left join、right join,优先使用Inner join,如果是left join,左边表结果尽量少 10.尽量避免

    Mysql联表update数据的示例详解

    [INNER JOIN | LEFT JOIN] T1 ON T1.C1 = T2. C1 SET T1.C2 = T2.C2, T2.C3 = expr WHERE condition 更详细地看看MySQL UPDATE JOIN语法: 首先,在UPDATE子句之后,指定主表(T1)和希望主表连接表(T2)。 第二,指定...

    Sql 语句详解

    17. SQL INNER JOIN 关键字 22 18. SQL LEFT JOIN 关键字 23 19. SQL RIGHT JOIN 关键字 24 20. SQL UNION 和 UNION ALL 操作符 26 21. SQL SELECT INTO 语句 28 22. SQL CREATE DATABASE 语句 29 23. SQL CREATE ...

    SQL语句生成及分析器(中文绿色)

    3.7.1 SQL 92 标准:Inner Join、Left Join、Right Join、Full Join, 3.7.2 特殊语法:*=、=*、*=*(MS_SQL,Sybase),(+)(Oracle) 3.8 联合 (Union [All],Minus,Intersect) 3.9 字段别名,数据表别名 ...

    SQL语句生成及分析器

    内连接(inner join)和外连接(left join/right join/full join) 排序(Order By) 条件(Where) 分组(Group By) 分组条件(Having) 计算字段 SQL查询表 SQL查询子句 丰富的函数 表别名 字段别名 联合(Union...

    sql语句生成与分析器.rar

    3.7.1 SQL 92 标准:Inner Join、Left Join、Right Join、Full Join, 3.7.2 特殊语法:*=、=*、*=*(MS_SQL,Sybase),(+)(Oracle) 3.8 联合 (Union [All],Minus,Intersect) 3.9 字段别名,数据表别名 ...

    数据库oracle各种连接(+)解释.pdf

    连接无非是这几个  --内连接和where相同 ... inner join  --左向外连接,  left join  --右向外连接,  right join  --完整外部连接,  full join  --交叉连接,也称笛卡儿积。 cross join .......

    JapaneseWordNet

    日语单词网 如何使用 运行下面的命令 ... sqlite3 wnjpn.db ...select word.lemma from sense inner join word on sense.wordid=word.wordid inner join synset on synset.synset=sense.synset where synset.name="ex

Global site tag (gtag.js) - Google Analytics