mysql 查询到 laravel orm
mysql query to laravel orm
我的目标是将以下 SQL 查询转换为 Laravel 查询构建器语法,
$qstA = "
select sum(tmpbutbldata.Personnel_Hours_Spent) as PHS from tmpbutbldata
left join tbl_testlocation_links on tmpbutbldata.Test_Request_Number= tbl_testlocation_links.Test_Request_number
where
tmpbutbldata.Date_Test_Completed between '".$dateBack."' and '".$dateCurr."'
and tbl_testlocation_links.".$Location."='1'
and tmpbutbldata.type = '1'
and tmpbutbldata.cancelled = '0'
or
tmpbutbldata.Date_Test_Completed between '".$dateBack."' and '".$dateCurr."'
and tbl_testlocation_links.".$Location."='1'
and tmpbutbldata.type = '2'
and tmpbutbldata.cancelled = '0'"
;
我尝试过的:
DB::table("tbldata")
->select(DB::raw('*'))
->leftJoin('tbl_testlocation_links', 'tbldata.Test_Request_Number', '=', 'tbl_testlocation_links.Test_Request_number')
->where(['Date_Test_Completed', '<', $dateBack],
['Date_Test_Completed', '>', $dateCurr],
['tbl_testlocation_links'.$location->TestLocation, '=', 1],
['type', '=', 1],
['cancelled', '=', 0])
->orWhere(['Date_Test_Completed', '<', $dateBack],
['Date_Test_Completed', '>', $dateCurr],
['tbl_testlocation_links'.$location->TestLocation, '=', 1],
['type', '=', 2],
['cancelled', '=', 0])
->get();
但是我得到以下错误ErrorException Array to string conversion
尝试
DB::table("tbldata")
->select(DB::raw('*'))
->leftJoin('tbl_testlocation_links', 'tbldata.Test_Request_Number', '=', 'tbl_testlocation_links.Test_Request_number')
->where([
['Date_Test_Completed', '<', $dateBack],
['Date_Test_Completed', '>', $dateCurr],
['tbl_testlocation_links'.$location->TestLocation, '=', 1],
['type', '=', 1],
['cancelled', '=', 0]
])
->orWhere([
['Date_Test_Completed', '<', $dateBack],
['Date_Test_Completed', '>', $dateCurr],
['tbl_testlocation_links'.$location->TestLocation, '=', 1],
['type', '=', 2],
['cancelled', '=', 0]
])
->get();
多个条件必须作为数组的数组在单个 where 子句中传递,其中每个嵌套数组代表一个条件
我的目标是将以下 SQL 查询转换为 Laravel 查询构建器语法,
$qstA = "
select sum(tmpbutbldata.Personnel_Hours_Spent) as PHS from tmpbutbldata
left join tbl_testlocation_links on tmpbutbldata.Test_Request_Number= tbl_testlocation_links.Test_Request_number
where
tmpbutbldata.Date_Test_Completed between '".$dateBack."' and '".$dateCurr."'
and tbl_testlocation_links.".$Location."='1'
and tmpbutbldata.type = '1'
and tmpbutbldata.cancelled = '0'
or
tmpbutbldata.Date_Test_Completed between '".$dateBack."' and '".$dateCurr."'
and tbl_testlocation_links.".$Location."='1'
and tmpbutbldata.type = '2'
and tmpbutbldata.cancelled = '0'"
;
我尝试过的:
DB::table("tbldata")
->select(DB::raw('*'))
->leftJoin('tbl_testlocation_links', 'tbldata.Test_Request_Number', '=', 'tbl_testlocation_links.Test_Request_number')
->where(['Date_Test_Completed', '<', $dateBack],
['Date_Test_Completed', '>', $dateCurr],
['tbl_testlocation_links'.$location->TestLocation, '=', 1],
['type', '=', 1],
['cancelled', '=', 0])
->orWhere(['Date_Test_Completed', '<', $dateBack],
['Date_Test_Completed', '>', $dateCurr],
['tbl_testlocation_links'.$location->TestLocation, '=', 1],
['type', '=', 2],
['cancelled', '=', 0])
->get();
但是我得到以下错误ErrorException Array to string conversion
尝试
DB::table("tbldata")
->select(DB::raw('*'))
->leftJoin('tbl_testlocation_links', 'tbldata.Test_Request_Number', '=', 'tbl_testlocation_links.Test_Request_number')
->where([
['Date_Test_Completed', '<', $dateBack],
['Date_Test_Completed', '>', $dateCurr],
['tbl_testlocation_links'.$location->TestLocation, '=', 1],
['type', '=', 1],
['cancelled', '=', 0]
])
->orWhere([
['Date_Test_Completed', '<', $dateBack],
['Date_Test_Completed', '>', $dateCurr],
['tbl_testlocation_links'.$location->TestLocation, '=', 1],
['type', '=', 2],
['cancelled', '=', 0]
])
->get();
多个条件必须作为数组的数组在单个 where 子句中传递,其中每个嵌套数组代表一个条件