Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.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 实左外连接_Mysql_Join - Fatal编程技术网

Mysql 实左外连接

Mysql 实左外连接,mysql,join,Mysql,Join,如何在mysql中执行真正的左外连接?似乎它的左外连接包括内连接。我需要在表a中查找不在表b中的记录 我能想出的最好办法就是 select * from `a` where `a`.`index` not in (select `index` from `b`) 有没有更优化的方法?没有子查询可能吗?这是使用左连接的方式: select * from `a` left outer join `b` on `a`.`index` = `b`.`index` where `b`.`index`

如何在mysql中执行真正的左外连接?似乎它的左外连接包括内连接。我需要在表
a
中查找不在表
b
中的记录
我能想出的最好办法就是

select * from `a` where `a`.`index` not in (select `index` from `b`)

有没有更优化的方法?没有子查询可能吗?

这是使用左连接的方式:

select * 
from `a`
left outer join `b`
on `a`.`index` = `b`.`index`
where `b`.`index` is null

您所描述的可能的副本不是左外连接。左联接包含表
A
中的所有记录,无论表
b
中是否有匹配的记录。我建议使用
不存在
而不是
不在
中。我同意这一点,但是不止一位DBA告诉我要避免这种类型的查询,这与数据库引擎无法正确优化这一点有关,因此性能会受到影响。我不知道这是不是真的,但至少值得对其他方法进行一些性能测试。@Joe,这真的取决于数据库。。。MySql在子查询方面也不是很好,所以我认为这一点也不重要。@jswolf19:这很有意义。我的建议来自SQL Server和Oracle DBA,但发现MySQL的优化器表现不同并不奇怪。