使用Laravel

时间:2019-03-05 23:54:54

标签: php mysql json laravel

我的数据以JSON数组的形式存储在from列中,如下所示:

[{"emailAddress": {"name": "Test", "address": "example@example.com"}}]

我想在该列中“不区分大小写”搜索。这是我到目前为止尝试过的(没有成功):

->orWhereRaw('lower(from->"$[*].emailAddress.name") like lower(?)', ["%{$searchTerm}%"]);

->orWhereRaw('lower(from->"$.emailAddress.name") like lower(?)', ["%{$searchTerm}%"]);

->orWhereRaw('lower(from->emailAddress"$.name") like lower(?)', ["%{$searchTerm}%"]);

所有具有相同错误:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from->"$[*].emailAddress.name") like lower(?)' at line 1

“区分大小写的查询”

->orWhere('from->emailAddress->name', 'like', "{$searchTerm}");

有效

我正在使用MySql 5.7.25和Laravel 5.8。

1 个答案:

答案 0 :(得分:2)

from是SQL中的特殊关键字,必须用反引号引起来。最可能起作用的查询版本是:

lower(`from`->"$[*].emailAddress.name") like lower(?)
相关问题