【问题标题】:Laravel != operator in where not workingLaravel != 运算符在不工作的地方
【发布时间】:2014-06-09 05:21:20
【问题描述】:

当需要一个对象时,此查询返回 null。

$vow = DB::table('media_featured')->where('is_video_of_the_week', 1)->
where('video_of_week_expired', '!=', 1)->first();

CREATE TABLE `media_featured` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`media_id` int(10) unsigned DEFAULT NULL,
`is_video_of_the_week` tinyint(1) DEFAULT NULL,
`is_featured` tinyint(1) DEFAULT NULL,
`video_of_week_expired` tinyint(1) DEFAULT NULL,
`featured_expired` tinyint(1) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `media_featured_media_id_foreign` (`media_id`),
CONSTRAINT `media_featured_media_id_foreign` FOREIGN KEY (`media_id`) REFERENCES `media`        (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

一条记录可能有is_video_of_the_week = 1video_of_week_expired = NULL,但上面的查询返回null。

有什么想法吗?

【问题讨论】:

    标签: php mysql laravel-4


    【解决方案1】:

    NULL 值不等于或不等于其他任何值。

    所以column != NULLcolumn = NULL 一样总是虚假的

    要检查列是否包含NULL 值,您需要使用IS NULL 运算符。

    如果是 laravel db 查询生成器,您可以使用

    ->whereNull('video_of_week_expired')
    

    方法。

    PS:如果video_of_week_expired 被假定为类似标志的列,则最好将其设为NOT NULL 并使用0/1 值,而不是NULL/1

    【讨论】:

    • 在我发布到 SO 之前,上述内容确实对我有用。我计划在不再显示该列时将其标记为 is_expired = 1,这就是为什么我想与 not =1 进行比较,而不是 whereNull,但我想我在代码执行中只需要小心吗?跨度>
    【解决方案2】:

    如果video_of_week_expired 的值为NULL1,那么您可以使用

    ->whereNull()

    否则 如果该值类似于标志01,那么您可以尝试使用

    ->where('video_of_week_expired', '', 1)

    这里<> 是一个“不等于”运算符。

    【讨论】:

      【解决方案3】:

      基于documentationsource-code

      你应该使用whereNull:

      $vow = DB::table('media_featured')
                ->where('is_video_of_the_week', 1)
                ->whereNull('video_of_week_expired')
                ->first();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-12
        • 1970-01-01
        • 2013-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-09
        相关资源
        最近更新 更多