php laravel的CROSS APPLY OPENJSON查询是什么?

gkn4icbw  于 2023-01-01  发布在  PHP
关注(0)|答案(2)|浏览(133)

我正在使用这个查询。
在数据库结构中将column_name转换为Json格式如下:

[
    {
        "Date": "2022-11-14T10:45:05.424+01:00",
        "Name": "nilesh1",
        "Level": "1"
    },
    {
        "Date": "2022-11-14T10:54:15.776+01:00",
        "Name": "nilesh2",
        "Level": "2"
    }
]

我不得不使用以下查询与Laravel雄辩:

select [customername] as [customerName]
from [opf_details] 
CROSS APPLY OPENJSON (column_name,'$') where JSON_VALUE(value,'$.Name') LIKE '%nilesh1%'
pzfprimi

pzfprimi1#

我是这样做的:

myModel::whereRaw('JSON_CONTAINS(`column_name`, '\{"Name":"nilesh2"}\')')
67up9zun

67up9zun2#

您可以将whereRaw()crossJoin()一起使用

$results = DB::table('opf_details')
    ->select('customername as customerName')
    ->whereRaw("JSON_VALUE(column_name, '$.Name') LIKE '%nilesh1%'")
    ->crossJoin(DB::raw("OPENJSON(column_name, '$')"))
    ->get();
  1. crossJoin()
  2. whereRaw()

编辑

$customerName = DB::table('opf_details')
    ->selectRaw('customername as customerName')
    ->crossJoin(DB::raw("OPENJSON (column_name,'$')"))
    ->whereRaw("JSON_VALUE(value,'$.Name') LIKE '%nilesh1%'")
    ->get();

相关问题