问题
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