0

以下代码应该返回与另一个表中的参数匹配或根本不匹配 ID 的结果。

这是我的尝试,我不明白为什么它不起作用。它返回结果数的许多倍。

SELECT *
  FROM table2, table1
  WHERE table1.ID = table2.name 
        AND table1.value = 1 
        OR table1.ID != table2.name

如何解决这个问题

4

2 回答 2

0

Looks like you need some parenthesis

FROM table2, table1 WHERE (table1.ID = table2.name AND table1.value = 1) OR table1.ID != table2.name

FROM table2, table1 WHERE table1.ID = table2.name AND (table1.value = 1 OR table1.ID != table2.name)
于 2012-06-29T07:07:27.313 回答
0

You can do two things:

  • A left outer join (or right)
  • Or a full outer join

Look to here for an explanation of them: http://www.w3resource.com/sql/joins/perform-a-full-outer-join.php

SELECT *
  FROM table2, table1
  WHERE table1.ID = table2.name(+) 
        AND table1.value = 1 

An example up, there, untested but I think it'll work

于 2012-06-29T07:08:15.787 回答