博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL 中 关于Left Join 转为 Inner Join 的问题
阅读量:6278 次
发布时间:2019-06-22

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

结论

从下面的测试结果很容易看出,左关联的查询语句中,只要有 WHERE 的过滤条件,那么该语句将被转为内关联。

一、创建表

演示使用,随便创建两张表

--班级表CREATE TABLE T_CLASS(  class_id NUMBER not null,  class_name VARCHAR2(100))--学生表CREATE TABLE T_STUDENT(  student_id NUMBER not null,  class_id NUMBER not null,  student_name VARCHAR2(100),  age NUMBER,  sex CHAR(1) )

二、创建数据

--班级数据insert into T_CLASS (CLASS_ID, CLASS_NAME)values (1, '一班');insert into T_CLASS (CLASS_ID, CLASS_NAME)values (2, '二班');insert into T_CLASS (CLASS_ID, CLASS_NAME)values (3, '三班');insert into T_CLASS (CLASS_ID, CLASS_NAME)values (4, '四班');insert into T_CLASS (CLASS_ID, CLASS_NAME)values (5, '五班');--学生数据insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)values (1, 1, '李1', 3, '1');insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)values (2, 1, '李2', 2, '1');insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)values (3, 1, '李3', 3, '1');insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)values (4, 2, '李4', 4, '1');insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)values (5, 2, '李5', 3, '2');insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)values (6, 2, '李6', 3, '1');insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)values (7, 3, '李7', 6, '2');insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)values (8, 3, '李8', 4, '2');insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)values (9, 2, '李9', 2, '2');insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)values (10, 2, '李10', 3, '1');insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)values (11, 3, '李11', 3, '2');insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)values (12, 2, '李12', 8, '2');insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)values (13, 1, '李13', 6, '2');

三、左关联出现的几种情形

1.无过滤条件

查询语句:

SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID ORDER BY A.CLASS_ID;

查询结果:

_

2.A表过滤条件在AND中

查询语句:

SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID   AND A.CLASS_ID = 1 ORDER BY A.CLASS_ID;

查询结果:

A_AND_

3.A表过滤条件在WHERE中

查询语句:

SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID WHERE A.CLASS_ID = 1 ORDER BY A.CLASS_ID;

查询结果:

A_WHERE_

4.B表过滤条件在AND中

查询语句:

SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID   AND B.AGE = 3 ORDER BY A.CLASS_ID;

查询结果:

B_AND_

5.B表过滤条件在WHERE中

查询语句:

SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID WHERE B.AGE = 3 ORDER BY A.CLASS_ID;

查询结果:

B_WHERE_

6.A表过滤条件在AND中,B表过滤条件在WHERE中

查询语句:

SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID   AND A.CLASS_ID = 1 WHERE B.AGE = 3 ORDER BY A.CLASS_ID;

查询结果:

A_AND_B_WHERE_

7.A表过滤条件在WHERE中,B表过滤条件在AND中

查询语句:

SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID   AND B.AGE = 3 WHERE A.CLASS_ID = 1 ORDER BY A.CLASS_ID;

查询结果:

A_WHERE_B_AND_

8.A表和B表过滤条件都在AND中

查询语句:

SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID   AND B.AGE = 3   AND A.CLASS_ID = 1 ORDER BY A.CLASS_ID;

查询结果:

A_B_AND_

9.A表和B表过滤条件都在WHERE中

查询语句:

SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID WHERE B.AGE = 3   AND A.CLASS_ID = 1 ORDER BY A.CLASS_ID;

查询结果:

A_B_WHERE_

以下附上所有查询语句:

--1.无过滤条件SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID ORDER BY A.CLASS_ID;--2.A表过滤条件在AND中SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID   AND A.CLASS_ID = 1 ORDER BY A.CLASS_ID;--3.A表过滤条件在WHERE中SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID WHERE A.CLASS_ID = 1 ORDER BY A.CLASS_ID;--4.B表过滤条件在AND中SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID   AND B.AGE = 3 ORDER BY A.CLASS_ID;--5.B表过滤条件在WHERE中SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID WHERE B.AGE = 3 ORDER BY A.CLASS_ID;--6.A表过滤条件在AND中,B表过滤条件在WHERE中SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID   AND A.CLASS_ID = 1 WHERE B.AGE = 3 ORDER BY A.CLASS_ID;--7.A表过滤条件在WHERE中,B表过滤条件在AND中SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID   AND B.AGE = 3 WHERE A.CLASS_ID = 1 ORDER BY A.CLASS_ID;--8.A表和B表过滤条件都在AND中SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID   AND B.AGE = 3   AND A.CLASS_ID = 1 ORDER BY A.CLASS_ID;--9.A表和B表过滤条件都在WHERE中SELECT *  FROM T_CLASS A  LEFT JOIN T_STUDENT B    ON A.CLASS_ID = B.CLASS_ID WHERE B.AGE = 3   AND A.CLASS_ID = 1 ORDER BY A.CLASS_ID;

转载地址:http://xxfva.baihongyu.com/

你可能感兴趣的文章
沉迷AC自动机无法自拔之:[BZOJ2434] [Noi2011] 阿狸的打字机
查看>>
图说Hadoop HA
查看>>
线程线程杂谈(1)
查看>>
cell左右滑动展开更多按钮-MGSwipeTableCell
查看>>
git 终端常输入命令
查看>>
前端开发学习笔记(二)
查看>>
JAVA设计模式:状态模式
查看>>
pom.xml
查看>>
快速幂模版
查看>>
JavaScript中的匿名函数及函数的闭包
查看>>
karma + phantom + mocha + sion + chai + nightwatch + selenium2(webdriver) 测试框架学习
查看>>
C#操作EXCEL类
查看>>
用java构造一个带层次的文件目录遍历器
查看>>
BZOJ2095:[POI2010]Bridges(最大流,欧拉图)
查看>>
6.计算字段 ---SQL
查看>>
js 对象深拷贝
查看>>
第二次作业
查看>>
WHERE 与HAVING 区别
查看>>
REUSE_ALV_POPUP_TO_SELECT的使用技巧
查看>>
Codeforces 551 E - GukiZ and GukiZiana
查看>>