`

left join on and

 
阅读更多
数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。
      在使用left jion时,on和where条件的区别如下:
1、 on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。
2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。
       假设有两张表:


CREATE TABLE t1
(
	id INT,
	SIZE INT
)

CREATE TABLE t2
(   
	id INT,
	NAME VARCHAR(10) 
) 

INSERT INTO t1 VALUES (1,10),(2,20),(3,30)
INSERT INTO t2 VALUES (10,'AAA'),(20,'BBB'),(20,'CCC')

表1 tab1:
id size
1 10
2 20
3 30
表2 tab2:
size name
10 AAA
20 BBB
20 CCC

两条SQL:
1、select * form tab1 left join tab2 on (tab1.size = tab2.size) where tab2.name=’AAA’
2、select * form tab1 left join tab2 on (tab1.size = tab2.size and tab2.name=’AAA’)
第一条SQL的过程:
1、中间表
on条件: 
tab1.size = tab2.size
tab1.id    tab1.size    tab2.size     tab2.name
1             10          10             AAA
2             20          20             BBB
2             20          20             CCC
3             30        (null)          (null)
2、再对中间表过滤
where 条件:
tab2.name=’AAA’
tab1.id       tab1.size        tab2.size     tab2.name
1              10                  10            AAA
第二条SQL的过程:
1、中间表
on条件: 
tab1.size = tab2.size and tab2.name=’AAA’
(条件不为真也会返回左表中的记录)
tab1.id      tab1.size         tab2.size       tab2.name
1               10                  10           AAA
2               20                 (null)       (null)
3               30                 (null)       (null)
第三条SQL语句:
SELECT * FROM T1 LEFT JOIN T2 ON T1.SIZE=T2.id 
结果集:
1	10	10	AAA
2	20	20	BBB
2	20	20	CCC
3	30	NULL	NULL

如果改为inner join and(where)  情况如下
SELECT * FROM T1 INNER JOIN T2 ON T1.SIZE=T2.id AND T2.[NAME]='AAA'

id	SIZE	id	NAME
1	10	 10	 AAA

   其实以上结果的关键原因就是left join,right join,full join的特殊性,不管on上的条件是否为真都会返回left或right表中的记录,full则具有left和right的特性的并集。 而inner jion没这个特殊性,则条件放在on中和where中,返回的结果集是相同的。
分享到:
评论

相关推荐

    left_join_on_and与left_join_on_where的区别

    left_join_on_and与left_join_on_where的区别

    MySQL IF、 Bewteen、 AS、 Date_Format、Left join on等关键字实例

    我们在写SQL时候,往往会需要格式化或多表联合查询,现在来一个实例看看这个问题 SELECT t.id, t.topic_...LEFT JOIN student_commit_history sch on t.id = sch.topic_id where sch.student_id =41 AND sch.create_t

    oracle join on 数据过滤问题

    代码如下:select a.f_username from ( SELECT /*+parallel(gu,4)*/distinct gu.f_username FROM t_base_...playid=4 and gu.f_paymoney>=1500 ) A left join ( select from t_base_vip_customes and ((vu.f_passeddate

    深入理解mysql之left join 使用详解

    即使你认为自己已对 MySQL 的 LEFT JOIN 理解深刻,但我敢打赌,这篇文章肯定能让你学会点东西

    Hands-On Data Science and Python Machine Learning

    Join Frank Kane, who worked on Amazon and IMDb's machine learning algorithms, as he guides you on your first steps into the world of data science. Hands-On Data Science and Python Machine Learning ...

    优化临时表使用,SQL语句性能提升100倍

     SELECT DISTINCT g.*, cp.name AS cp_name, c.name AS category_name, t.name AS type_name FROMgm_game g LEFT JOIN gm_cp cp ON cp.id = g.cp_id AND cp.deleted = 0 LEFT JOIN gm_category c ON c.id = g....

    天思产品触发器

    update a set a.fs=a.kd*2/@spc from tf_pos_z a left join tf_pos c on a.os_no=c.os_no and a.itm=c.itm left join prdt b on c.prd_no=b.prd_no left join inserted d on a.os_no=d.os_no and a.itm=d.itm ...

    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...

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

    table a(id, type):id type ———————————-1 1 2 1 3 2 table b(id, class):id class ———————————1 12 2sql语句1:select a.*, b.* from a left join b on a.id = b.id and a.type = ...

    易飞ERP体检SQL

    鼎捷易飞ERP体检SQL,如:产品类别订单准交率,月度接单和销货金额,工单平均生产周期,工单准时完工率,车间工单准时开工率,... left join COPTD ON TC001=TD001 AND TC002=TD002 left join INVMB ON TD004=MB001 LE

    优化sql的测试数据

    LEFT JOIN TM_Product_Mapping tpm ON PI.ID = tpm.ProductID and tpm.Status <> 2 LEFT JOIN TM_Account_Info tmi ON tpm.AccountID = tmi.ID LEFT JOIN (select sum(if(`Status` =1,1,0))as OnLineNum,sum(if(`...

    SQL where条件和jion on条件的详解及区别

    FROM TBL_StockMaterial RIGHT OUTER JOIN TBL_SchemaDetail ON TBL_StockMaterial.MaterialNo = TBL_SchemaDetail.MaterialNo AND TBL_SchemaDetail.SchemaNo = '7411' AND TBL_StockMaterial.Sa

    mysql查询必练50题

    LEFT JOIN SC b ON b.cid=01 AND a.sid=b.sid LEFT JOIN SC c ON c.cid=02 AND a.sid=c.sid WHERE b.score>c.score; -- 2、查询平均成绩大于60分的同学的学号和平均成绩; SELECT sid 学号, AVG(score) 平均成绩...

    SBO发出商品报表

    from oinm t0 left join DLN1 t1 on t0.DocLineNum=t1.LineNum and t0.CreatedBy=t1.DocEntry left join odln t2 on T2.[DocEntry] = T1.[DocEntry] where t0.CostAct not in ('540101' , '540102' ) and t0....

    laravel高级的Join语法详解以及使用Join多个条件

    在laravel中我们常常会使用join,leftjion和rightjoin进行连表...select * from `orders` left join `users` on `orders`.`usename`=`users`.`usename` and `orders`.`platform`=`users`.`platform` where `orders`.`or

    SQL Server 交叉表查询 case

    access代码: 代码如下: TRANSFORM First(Landundertake.valuerId) AS valuerId之First SELECT Appraiser.quarterId, Landundertake.landCode FROM Landundertake INNER JOIN Appraiser ON (Landundertake .valuerId...

    SQLQuery5.sql

    from score a left join ( select a.s_score from Score a group by a.s_score having count(a.s_score)>1 and count(distinct a.c_id) > 1)tt on tt.s_score=a.s_score group by a.s_id select distinct a.s_...

    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 (@...

Global site tag (gtag.js) - Google Analytics