我在 mysql 5.7.11 上运行了一个数据库,并且将大量数据放入带有 JSON 列的表中。我得到了一个像这样的原始查询。
select * from `note`
where `note_structure_id` = 3
and JSON_EXTRACT(LCASE(data), '$._letter') = 'a'
and JSON_EXTRACT(data, '$._number') = 1
在 sequel pro 或 phpmyadmin 中运行它给了我我期望的结果;
{"_letter": "A", "_number": 1}
我用 laravel 构建的相同查询它给我的结果是什么。一个空数组。我构建相同查询的代码得到了这种代码。
$result = \DB::table( 'note' )->where( 'note_structure_id', $structureId );
if( is_array( $query ) ) {
if( isset( $query['where'] ) ) {
foreach( $query['where'] AS $field => $value ) {
if( is_numeric( $value ) ) {
$result = $result->where( \DB::raw( "JSON_EXTRACT(data, '$._{$field}')" ), '=', $value );
}
else {
$result = $result->where( \DB::raw( "JSON_EXTRACT(LCASE(data), '$._{$field}')" ), '=', strtolower( $value ) );
}
}
}
}
dd($result->get());
有谁知道我的代码做错了什么。我尽一切可能修复它,但没有结果。
谢谢!