Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 左连接(表1,表2)和左连接表1左连接表2之间的区别是什么_Mysql_Sql_Database - Fatal编程技术网

Mysql 左连接(表1,表2)和左连接表1左连接表2之间的区别是什么

Mysql 左连接(表1,表2)和左连接表1左连接表2之间的区别是什么,mysql,sql,database,Mysql,Sql,Database,这两个sql查询之间的区别是什么 select * from a left join (b, c) on a.id = b.uid and a.id = c.uid select * from a left join b on a.id = b.uid left join c on a.id = c.uid 相当于 select * from a left join (b cross join c) on (a.id = b.uid and a.id = c.uid) 在这里您可以找到详

这两个sql查询之间的区别是什么

select * from a
left join (b, c)
on a.id = b.uid and a.id = c.uid

select * from a
left join b on a.id = b.uid
left join c on a.id = c.uid
相当于

select * from a 
left join (b cross join c)
on (a.id = b.uid and a.id = c.uid)
在这里您可以找到详细信息 让我们来看看这些数据:

A       B        C
id      uid      uid
--      ---      ---
 1        1        2
 2

第一,第二个查询:

select * from a
left join b on a.id = b.uid
left join c on a.id = c.uid

 ID UID  UID
 -- ---- ----
  1    1 NULL
  2 NULL    2
这并不奇怪-第二列从
b
连接,如果
b
中没有数据,则使用
NULL
(外部连接);第三列的行为相同,只适用于
c


第一个查询被重写为符合ANSI标准,并使用
交叉连接
(相当于此):

select * from a
left join (b CROSS JOIN c)
on a.id = b.uid and a.id = c.uid

 ID UID  UID
 -- ---- ----
  2 NULL NULL
  1 NULL NULL
为什么所有的
NULL
s都存在

首先,执行交叉联接,但这将导致仅包含一行的结果集:

b.UID   c.UID
-----   -----
    1       2

然后,执行左连接,但是交叉连接的结果中没有一行对
b
c
具有相同的
uid
,因此,在
a

中,任何一行都不能与任何一行匹配。第一个查询不是有效的ANSI SQL。它在mysql5.7中工作。@如果您不需要额外的努力就可以更正,那么编写不可移植的代码仍然是一个坏主意。如果您运行
解释扩展…
然后运行
显示警告它应该能让您更清楚地了解任何功能差异。谢谢,我会试试。@strawberry谢谢。我能从a得到结果吗?即使b或c在我的第二个sql中没有额外的数据?我想,你从@Jiri tosek得到了答案
b.UID   c.UID
-----   -----
    1       2