My where statement with a JSON_extract function on a nullable column is confirmed when said column's value is NULL?

泄露秘密 提交于 2021-01-27 19:37:02

问题


I have a where statement with a JSON_extract method in it. The JSON_extract uses a nullable column called new_value or old_value. The check works if the column contains a JSON string, but when the column is NULL the where statement gets confirmed.

->where(function($query) use ($other_key, $new_change) {
        $query->where(DB::raw('json_extract(old_value, "$.theme_id") = 1))
            ->orWhere(DB::raw('json_extract(new_value, "$.theme_id") = 1));

When the new_value or the old_value is NULL the row gets returned, but the theme_id of NULL obviously isn't equal to 1. Can someone explain what is happening here?


回答1:


This:

->where(function($query) use ($other_key, $new_change) {
    $query->where(DB::raw('json_extract(old_value, "$.theme_id") = 1))
        ->orWhere(DB::raw('json_extract(new_value, "$.theme_id") = 1));

Should be:

->where(function($query) use ($other_key, $new_change) {
    $query->where(DB::raw("json_extract(old_value, '$.theme_id')"), 1);
        ->orWhere(DB::raw("json_extract(new_value, '$.theme_id')"), 1);

That's how the where statements in Laravel work. I did not insert a second parameter and that's why Laravel assumed I was checking for NULL. It works now, sorry for my stupidity.



来源:https://stackoverflow.com/questions/42023802/my-where-statement-with-a-json-extract-function-on-a-nullable-column-is-confirme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!