我有一个SQL Server查询,其中包含带有连接的子查询。但是我不知道如何转换为laravel元素查询。
我的SQL查询:
SELECT id, fName, lName, gender, departmentName
FROM dbo.real_users c
right JOIN (
SELECT access_logs.real_user_fr FROM [access_logs]
WHERE [access_logs].[date] >= '2019-10-21 03:00:00' AND
[access_logs].[date] <= '2019-10-22 03:00:00'
GROUP BY [access_logs].[real_user_fr]
HAVING COUNT ( * ) % 2 = 1 ) present
ON c.id =present.real_user_fr
我在laravel中尝试了此代码,但无法正常工作。
$subquery = DB::table('access_logs')
->select(DB::raw("access_logs.real_user_fr"))
->groupBy('real_user_fr')->havingRaw("count('access_logs.*' ) % 2 ==
1");
if ($request['startDate']) {
$startDate = \App\Helpers\shamsiToMiladi($request['startDate']);
if ($request['startHour']) {
$startHour = $startDate.' '.$request['startHour'].':00';
$subquery->where('access_logs.date', '>=', '2019-10-21 03:00:00');
}
if ($request['endHour']) {
$endHour = $startDate.' '.$request['endHour'].':00';
$subquery = $subquery->where('access_logs.date', '<=','2019-10-22 03:00:00' );
}else{
$endHour = $startDate.' '.'23:59:00';
$subquery = $subquery->where('access_logs.date', '<=','2019-10-22 03:00:00' );
}
}
如何将其转换为laravel elequent?