mnv*_*mnv 0 php postgresql pdo laravel laravel-5.3
我有my_table带有jsonb列的表sentiments。我需要从此列中删除所有键“10216”和“10191”。
我尝试在 Laravel 中做下一步:
DB::table('my_table')
->whereRaw("sentiments ?| ARRAY['10216', '10191']")
->update([
'sentiments' => DB::raw("sentiments - '10216' - '10191'")
]);
Run Code Online (Sandbox Code Playgroud)
但我有下一个错误:
[Illuminate\Database\QueryException]
SQLSTATE[42601]: Syntax error: 7 ??????: ?????? ?????????? (????????? ?????????: "$1")
LINE 1: ...= sentiments - '10216' - '10191' where sentiments $1| ARRAY[...
^ (SQL: update "my_table"
set "sentiments" = sentiments - '10216' - '10191'
where sentiments ?| ARRAY['10216', '10191'])
Run Code Online (Sandbox Code Playgroud)
因为正如我所看到的“?” 看起来像参数。如何逃避这个符号?
更新
我也试着写两个问题sentiments ??| ARRAY::
[Illuminate\Database\QueryException]
SQLSTATE[42883]: Undefined function: 7 ??????: ???????? ?? ??????????: jsonb ??| text[]
LINE 1: ...= sentiments - '10216' - '10191' where sentiments ??| ARRAY[...
^
HINT: ???????? ? ??????? ?????? ? ?????? ?????????? ?? ??????. ????????, ??? ??????? ???????? ????? ?????????? ???
??. (SQL: update "my_table" set "sentiments" = sentiments - '10216' - '10191' where sentiments ??|
ARRAY['10216', '10191'])
Run Code Online (Sandbox Code Playgroud)
感谢@degr!它适用于未记录的功能jsonb_exists_any():
DB::table('my_table')
->whereRaw("jsonb_exists_any(sentiments, ARRAY['10216', '10191'])")
->update([
'sentiments' => DB::raw("sentiments - '10216' - '10191'")
]);
Run Code Online (Sandbox Code Playgroud)
更新
与使用 operator 不同,使用这种方法不使用 jsonb 字段上的索引?|。