【问题标题】:MySQL WHERE x OR IS NULL not workingMySQL WHERE x OR IS NULL 不起作用
【发布时间】:2016-06-30 02:12:04
【问题描述】:
select * from inventory.home_picture_types hpt
left join inventory.home_pictures hp on hp.home_picture_type_id = hpt.id
where hp.id IS NULL;

此查询适用于 vagrant/homestead,并返回 home_picture_types 的 home_picture 值为 NULL(正确,我只是想获取 home_picture_types)。

但是,当我在 CentOS Maria DB 上运行此查询时,它根本没有返回任何结果。 好像 LEFT JOIN 是 INNER JOIN。

我已经用 700 种不同的方式编写了这个查询,但没有任何帮助。我检查了 sql_mode,它在两个数据库上都设置为 ''....任何想法为什么它适用于一个而不是另一个??

【问题讨论】:

  • NULL 永远不等于任何东西,包括它自己。你的 SQL 没有意义。
  • select * from inventory.home_picture_types hpt left join inventory.home_pictures hp on hp.home_picture_type_id = hpt.id where hp.id=''; 试试这个。
  • 它应该返回没有匹配 hpt.id 和 hp.home_picture_type 的行,对吗?我试图找到他们不匹配的地方,这只会返回类似:tinypic.com/r/243pzti/9
  • 你的建议没有返回任何内容,就像 = NULL 一样,但我一开始并没有尝试过..
  • 你确定数据库有相同的数据?我没有发现您的查询有任何问题...

标签: mysql centos where mariadb isnull


【解决方案1】:

所有类型:select DISTINCT home_picture_type_id from inventory.home_picture_types

没有图片的类型:select home_picture_type_id from inventory.home_picture_types AS hpt WHERE NOT EXISTS( SELECT * FROM inventory.home_pictures WHERE home_picture_type_id = hpt.id ).

【讨论】:

    猜你喜欢
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多