【问题标题】:Select from two outer joined tables where a possible null column从可能存在空列的两个外部连接表中选择
【发布时间】:2012-06-25 07:45:46
【问题描述】:

类似的问题:

select from two tables where linked column can be null

column1tab1 column2tab1 order_number product amount
 xx            yy            123      p1      2
 xx            yy            456      p3      4
 xx            yy            NULL    NULL    NULL
 xx            yy            789      p2      1
 etc...

海报的输出如上。但是,当我添加 WHERE product = 'p1' 或 WHERE product = NULL 时,两者都返回一个空集。最后,我想拥有

SELECT            *
FROM              `t1`
  LEFT OUTER JOIN `t2`
  ON              (`t1`.`id` = `t2`.`id2`)
WHERE             `t2`.`product` = NULL
  AND             `t2`.`product` <> 'p1'

我做错了哪一部分?加入还是在哪里?还是别的什么?

【问题讨论】:

  • product = NULL 将是 product IS NULL,但除此之外,空值会自动使第二个 product &lt;&gt; 'p1' 为真。

标签: mysql left-join outer-join


【解决方案1】:

替换

`t2`.`product` = NULL

`t2`.`product` IS NULL

See here

【讨论】:

    【解决方案2】:

    t2.product = NULL 始终为假。请改用t2.product IS NULL

    【讨论】:

      【解决方案3】:

      如果 t2.product 为 null,它总是不同于字符串 'p1'。我认为不需要 and。

      【讨论】:

        猜你喜欢
        • 2016-08-02
        • 1970-01-01
        • 1970-01-01
        • 2019-04-09
        • 1970-01-01
        • 2017-04-10
        • 1970-01-01
        • 1970-01-01
        • 2015-12-05
        相关资源
        最近更新 更多