我需要做的查询是这样的
select `id` ,
json_extract(meta,"$.A") as a,
json_extract(meta,"$.B") as b
from `C`
where json_unquote(json_extract(meta, '$."A"'))>
json_unquote(json_extract(meta,'$.B'))
在workbench上运行它会得到2个结果
Results
ID a
414 2000 1500
426 2000 1500
但是当我把它传递给eloquent的形式时,结果是空的
DB::table('C')->select('meta->A')->
where('meta->A','>',"json_unquote(json_extract(meta,'$.B'))")->get();
调试库,我看到在PDO函数中,不知何故它没有正确绑定参数
array:9 [
"select" => []
"from" => []
"join" => []
"where" => array:1 [
0 => "json_unquote(json_extract(meta,'$.B'))"
]
"groupBy" => []
"having" => []
"order" => []
"union" => []
"unionOrder" => []
]
array:1 [
0 => "json_unquote(json_extract(meta,'$.B'))"
]
"statement bind value"
PDOStatement {#3745
+queryString: "select json_unquote(json_extract(`meta`, '$."A"')) from `C` where json_unquote(json_extract(`meta`, '$."A"')) > ?"
}
我如何修改它来完成我需要的查询?,我尝试使用whereRaw,但我有同样的问题与绑定
问题很可能是你的raw-query没有使用mysql的内置函数,因为实际上不是那么原始(仍在构建中)。
你可以通过删除构建器来使查询完全原始,或者更好的是,使用laravel本身的JSON功能。
Laravel Queries - JSON where子句
类似以下语句的内容:
DB::table('C')->select('meta->A')
->whereJsonContains('meta->A', '$.B')
->get();