我需要执行的查询如下所示
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'))在workbrench上运行它会得到2个结果
Results
ID a
414 2000 1500
426 2000 1500但是当我将它传递给eloquent的orm时,结果是空的。
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,但是我在绑定时遇到了同样的问题
发布于 2021-08-21 17:09:43
问题很可能是您的原始查询没有使用mysql的内置函数,因为实际上没有使用原始查询(仍在构建中)。
您可以通过完全删除构建器来使查询成为实际的原始查询,或者更好的做法是使用laravel本身的JSON功能。
Laravel Queries - JSON where clauses
大致是这样的:
DB::table('C')->select('meta->A')
->whereJsonContains('meta->A', '$.B')
->get();https://stackoverflow.com/questions/68874812
复制相似问题