MySQL 加入未按要求工作

MySQL Joins not working as required

我有两张桌子。

Table答:

root_id, root_text

Table乙:

word_id , word_text,root_text

现在 Table B 的一些记录有 root_texts 可能不存在于 Table A.

为此,我正在尝试使用左连接。

select *
from   A a , B b
where  a.root_text=+b.root_text

我只得到符合root_text.

的记录

如果我用新的方式

Select
  *
from
  A left join B on a.root_text= b.root_text 

我得到了很多额外的记录。我正在使用 MySQL 5.6.1.7

更新:

http://sqlfiddle.com/#!9/7b32a/2

查询我是运行

select * from   word_detail w left  join roots r
on r.root_text =w.root
and w.surah_no=1
and w.verse_no=1
and w.word_no=1

我做错了什么?

结果您看到了很多不需要的记录。过滤器 verse_no 、word_no 不工作。

更新

问题出在左连接之后,我们必须使用 where

select * from   word_detail w left  join roots r
    on r.root_text =w.root
    where w.surah_no=1
    and w.verse_no=1
    and w.word_no=1

如果您想要表 B 中不存在于 table A 中的所有记录,您应该使用:

Select *
from
  B left join A
  on a.root_text= b.root_text
where
  a.root_text is null

或者,如果您想要相反的结果 - tableA 中不存在于 tableB 中的所有记录:

Select *
from
  A left join B
  on a.root_text= b.root_text
where
  b.root_text is null

顺便说一句,这不是 MySQL 上的左连接:

select * from A a , B b where a.root_text=+b.root_text

但结果将是一个简单的 INNER JOIN

+= 运算符不是标准 SQL,而是特定于 Oracle RDBMS,因此它不会按预期在 MySQL 上运行。

LEFT JOIN 语法正确,"many extra records" 源于您表中的数据。您可能希望使用某种 WHERE 作为过滤器或聚合来对结果集进行分组以使其更易于管理。

您的第一个示例是内部联接,它解释了为什么您没有获得与离开联接时一样多的结果。左连接也可以认为是左外连接。