JSON_CONTAINS 无法识别我的专栏 mysql v.8.0.17 laravel 5.6
JSON_CONTAINS doesn't recognize my column mysql v.8.0.17 laravel 5.6
我必须执行与此类似的查询:
SomeModel::whereRaw("JSON_CONTAINS(column, '', '$.a')")->get();
column contains JSON *{"a":null}*;
使用 mysql 版本 5.7 一切正常,我得到空集合但是在 digitalocean 上的数据库是 mysql v8.0.17 我得到这个错误:
Illuminate/Database/QueryException with message 'SQLSTATE[22032]: <>: 3141 Invalid JSON text in argument 1 to function json_contains: "The document is empty." at position 0. (SQL: select * from 'table' where JSON_CONTAINS(column, '', '$.a'))'
我被困在这个问题上好几天了,请帮忙! :)
问题是您要在 json 列中搜索 json 字段,因此您需要一个 json 数组,而不仅仅是一个空值。
试试这样的东西:
SomeModel::whereRaw("JSON_CONTAINS(`column`, cast('[]' as json), '$.a')")->get();
也使用 `` 作为您的列名。
由于您使用的是 laravel 5.7,您还可以使用 laravel 附带的 eloquent 函数,例如:
SomeModel::whereJsonContains('your_json_field','your_value')->get();
问题是您作为参数传递的空字符串。
尝试:
SomeModel::whereRaw("JSON_CONTAINS(column, null, '$.a')")->get();
我必须执行与此类似的查询:
SomeModel::whereRaw("JSON_CONTAINS(column, '', '$.a')")->get();
column contains JSON *{"a":null}*;
使用 mysql 版本 5.7 一切正常,我得到空集合但是在 digitalocean 上的数据库是 mysql v8.0.17 我得到这个错误:
Illuminate/Database/QueryException with message 'SQLSTATE[22032]: <>: 3141 Invalid JSON text in argument 1 to function json_contains: "The document is empty." at position 0. (SQL: select * from 'table' where JSON_CONTAINS(column, '', '$.a'))'
我被困在这个问题上好几天了,请帮忙! :)
问题是您要在 json 列中搜索 json 字段,因此您需要一个 json 数组,而不仅仅是一个空值。
试试这样的东西:
SomeModel::whereRaw("JSON_CONTAINS(`column`, cast('[]' as json), '$.a')")->get();
也使用 `` 作为您的列名。
由于您使用的是 laravel 5.7,您还可以使用 laravel 附带的 eloquent 函数,例如:
SomeModel::whereJsonContains('your_json_field','your_value')->get();
问题是您作为参数传递的空字符串。
尝试:
SomeModel::whereRaw("JSON_CONTAINS(column, null, '$.a')")->get();