Php 有没有办法将mysql转换为laravel雄辩的查询生成器?
我想获取员工当天的Php 有没有办法将mysql转换为laravel雄辩的查询生成器?,php,mysql,laravel,laravel-query-builder,Php,Mysql,Laravel,Laravel Query Builder,我想获取员工当天的首次签入和最后一次签出。下面的MySQL代码工作得很好,但我是个新手,不知道如何编写。对不起我的英语 SELECT `employees`.`*`, `teams`.`description`, `time_groups`.`start`, `time_groups`.`end`, cast(a1.action_time as date) AS date, Min(`a1`.`action_time`) AS `first_check_in`, MAX(`a2`.
首次签入
和最后一次签出
。下面的MySQL代码工作得很好,但我是个新手,不知道如何编写。对不起我的英语
SELECT `employees`.`*`,
`teams`.`description`,
`time_groups`.`start`,
`time_groups`.`end`,
cast(a1.action_time as date) AS date,
Min(`a1`.`action_time`) AS `first_check_in`,
MAX(`a2`.`action_time`) AS `last_check_out`
FROM `employees`
JOIN `teams`
ON `employees`.`team_id` = `teams`.`id`
JOIN `time_groups`
ON `teams`.`time_group_id` = `time_groups`.`id`
JOIN `attendance_employees` AS a1
JOIN `attendance_employees` AS a2
ON `employees`.`id` = `a2`.`employee_id`
AND `a1`.`employee_id` = `a2`.`employee_id`
AND DATE(a1.action_time) = DATE(a2.action_time)
WHERE 1 = 1
AND `a1`.`type` = 1
AND `a2`.`type` = 2
AND DATE(a1.action_time) = CURDATE()
GROUP BY a1.employee_id, `date`
我曾经试过这样的机会
DB::table('employees')
->join('teams', 'employees.team_id', '=', 'teams.id')
->join('time_groups', 'teams.time_group_id', '=', 'time_groups.id')
->join('attendance_employees as a1')
->join('attendance_employees as a2',
function($join) {
$join->on('employees.id', '=', 'a2.employee_id');
$join->on('a1.employee_id', '=', 'a2.employee_id');
$join->on('DATE(a1.action_time)', '=', 'DATE(a2.action_time)');
}
)
->select(
'employees.*',
'teams.description',
'time_groups.start',
'time_groups.end'
)
->addSelect('cast(A1.action_time as date)')->alias('date')
->addSelect('a1.action_time')->alias('check_in')
->addSelect('a2.action_time')->alias('check_out')
->where(1,1)
->where('a1.type', 1)
->where('a2.type', 2)
->groupBy('employee_id')
->groupBy('date')
但是得到以下错误:
SQLSTATE[42S02]:找不到基表或视图:1146表
“db.U员工作为a1”不存在
原始查询运行时是否没有任何错误?试试这些我有点好奇您如何能够运行它,因为
join
方法至少需要2个参数。这里的大部分繁重工作都可以通过模型之间的关系来完成。