使用(+)语法更改IS NULL

时间:2012-09-20 04:26:04

标签: sql join isnull

我有这个问题:

SELECT B.id, B.name, D.id, D.name FROM TBB B, TDD D
WHERE (D.id = B.id OR D.id IS NULL)

根据我的想法,(D.id = B.id OR D.id IS NULL)将显示在表TBB和TDD中都具有id的记录,但也显示所有B.id记录,即使两个表都没有相同的ID,因为D.id IS NULL

那么,这是否与上述查询相同:

 SELECT B.id, B.name, D.id, D.name FROM TBB B, TDD D
    WHERE B.id = D.id (+)

提前致谢!

2 个答案:

答案 0 :(得分:1)

第二个查询将返回TBB表中存在的所有行,而不是TDD表中的匹配列将返回为NULL。

in-case TDD表中有任何与TBB表ID不匹配的ID,该行不会通过上述两个查询返回。

答案 1 :(得分:0)

我很确定您的热门查询会导致B中每条记录的笛卡尔联接,因为查询中的or语句导致该记录为NULL。

基于此,你最好使用外连接。

我还编写了一个冗长的Q& A来查看联接以及如何从您可能感兴趣的多个表中提取数据(How can an SQL query return data from multiple tables)它涵盖了联合,内部和外部联接以及子查询。它有加载代码和输出结果,详细说明。 (到了我达到答案长度限制的点,所以不得不发布第二个答案)

编辑:运行快速测试后,这就是我想出的:

    mysql> select a.ID, a.Title, b.Name as Author
    from books a join authors b
    on a.authorID=b.ID or b.id=0;

    +----+----------------------+-------------------+
    | ID | Title                | Author            |
    +----+----------------------+-------------------+
    |  1 | Call of the Wild     | Fluffeh           |
    |  1 | Call of the Wild     | Jack London       |
    |  2 | Martin Eden          | Fluffeh           |
    |  2 | Martin Eden          | Jack London       |
    |  3 | Old Goriot           | Fluffeh           |
    |  3 | Old Goriot           | Honore de Balzac  |
    |  4 | Cousin Bette         | Fluffeh           |
    |  4 | Cousin Bette         | Honore de Balzac  |
    |  5 | Jew Suess            | Fluffeh           |
    |  5 | Jew Suess            | Lion Feuchtwanger |
    |  6 | Nana                 | Fluffeh           |
    |  6 | Nana                 | Emile Zola        |
    |  7 | The Belly of Paris   | Fluffeh           |
    |  7 | The Belly of Paris   | Emile Zola        |
    |  8 | In Cold blood        | Fluffeh           |
    |  8 | In Cold blood        | Truman Capote     |
    |  9 | Breakfast at Tiffany | Fluffeh           |
    |  9 | Breakfast at Tiffany | Truman Capote     |
    +----+----------------------+-------------------+
    18 rows in set (0.00 sec)

    mysql> select a.ID, a.Title, b.Name as Author
    from books a right outer join authors b
    on a.authorID=b.ID;
    +------+----------------------+-------------------+
    | ID   | Title                | Author            |
    +------+----------------------+-------------------+
    | NULL | NULL                 | Fluffeh           |
    |    1 | Call of the Wild     | Jack London       |
    |    2 | Martin Eden          | Jack London       |
    |    3 | Old Goriot           | Honore de Balzac  |
    |    4 | Cousin Bette         | Honore de Balzac  |
    |    5 | Jew Suess            | Lion Feuchtwanger |
    |    6 | Nana                 | Emile Zola        |
    |    7 | The Belly of Paris   | Emile Zola        |
    |    8 | In Cold blood        | Truman Capote     |
    |    9 | Breakfast at Tiffany | Truman Capote     |
    +------+----------------------+-------------------+
    10 rows in set (0.00 sec) 

这与外部联接肯定不同。正如我想的那样(至少在MySQL中)结果是第一个语句中的笛卡尔语,而不是外连接中的结果。