SQL按2个值联接,其中之一可以为空

时间:2019-06-05 14:13:22

标签: sql join null

我有2张桌子

+------------+-----------+-----------+
| itemid     | version   | anything  |
+------------+-----------+-----------+
| foo        | v0        |  blah     |
| foo        | v1        |  blah     |
| foo        | NULL      |  blah     |
| foo        | v2        |  meh      |
| bar        | v0        |  meh      |
| bar        | v1        |  24       |
| baz        | NULL      |  25       |
| qux        | NULL      |  26       |
+------------+-----------+-----------+

+------------+-----------+-----------+
| itemid     | version   | something |
+------------+-----------+-----------+
| foo        | v0        |  weck     |
| foo        | NULL      |  wock     |
| foo        | v2        |  weck     |
| bar        | v0        |  meck     |
| bar        | v1        |  cuack    |
| baz        | NULL      |  crack    |
| qux        | NULL      |  blah     |
+------------+-----------+-----------+

我需要通过itemidversion联接两个表,因此结果如下:

+------------+-----------+-----------+-----------+
| itemid     | version   | anything  | something |
+------------+-----------+-----------+-----------+
| foo        | v0        |  blah     |  weck     |
| foo        | v1        |  blah     |  NULL     |
| foo        | NULL      |  blah     |  wock     |
| foo        | v2        |  meh      |  weck     |
| bar        | v0        |  meh      |  meck     |
| bar        | v1        |  24       |  cuack    |
| baz        | NULL      |  25       |  crack    |
| qux        | NULL      |  26       |  blah     |
+------------+-----------+-----------+-----------+

我在做

SELECT t1.itemid,
       t1.version,
       t1.anything,
       t2.something
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.itemid=t2itemid AND t1.version=t2.version

它可以处理具有版本值的行,但是如果版本值为NULL,则具有空值的行如下所示:

+------------+-----------+-----------+-----------+
| itemid     | version   | anything  | something |
+------------+-----------+-----------+-----------+
| foo        | NULL      |  blah     |  NULL     |
| baz        | NULL      |  25       |  NULL     |
| qux        | NULL      |  26       |  NULL     |
+------------+-----------+-----------+-----------+

我尝试通过以下方式更改加入条件:

ON t1.itemid=t2.itemid AND (t1.version=t2.version OR ((t1.version is null) AND (t2.version is null)))

结果完全相同

我也尝试通过以下方式更改条件:

ON t1.itemid=t2.itemid AND (t1.version=t2.version OR ((isnull(t1.version,'-')) AND (isnull(t1.version,'-'))))

然后我收到错误消息:An expression of non-boolean type specified in a context where a condition is expected,near 'AND' [SQL State=S0001, DB Errorcode=4145]

如何连接具有相同itemid和NULL version的行?

编辑:也许这并不重要,但是在我真正的查询中,table1和table2是选择结果,对于该示例,我还跳过了第三个选择,其中还包含itemid和version。因此,这是select的联接。

2 个答案:

答案 0 :(得分:1)

在上一个示例中,您使用的ISNULL错误-请尝试-

   SELECT t1.itemid,
           t1.version,
           t1.anything,
           t2.something
    FROM table1 AS t1
    LEFT JOIN table2 AS t2
    ON t1.itemid=t2itemid AND ISNULL(t1.version, '-') = ISNULL(t2.version, '-')

答案 1 :(得分:0)

最后我找到了问题。似乎NULL字段并不总是NULL,有时NULL字段包含的东西不是NULL''' '

我不知道此字段确切包含什么,是不可见的字符。但是幸运的是,版本字段始终以V开头,因此我将此状态放置在版本字段中:

IIF (t1.version like 'V%', t1.version, NULL)

这样,我可以确保该字段包含一个NULL值,然后一切似乎都可以正常工作。

如果不是'V%',这些值应该始终为NULL,所以可能是由于谁在数据库中引入了行而引起的。

相关问题