美好的一天! 我是Laravel的新手。我尝试了很多方法,但它仍然给我一个错误。我试图将这个SQL查询转换成雄辩的
Select t.employee_code,
CASE WHEN t.day = '2017-08-19' THEN t.PRESENT ELSE NULL END AS `2017-08-19`,
CASE WHEN t.day = '2017-08-20' THEN t.PRESENT ELSE NULL END AS `2017-08-20`,
CASE WHEN t.day = '2017-08-21' THEN t.PRESENT ELSE NULL END AS `2017-08-21`,
CASE WHEN t.day = '2017-08-22' THEN t.PRESENT ELSE NULL END AS `2017-08-22`,
CASE WHEN t.day = '2017-08-23' THEN t.PRESENT ELSE NULL END AS `2017-08-23`,
CASE WHEN t.day = '2017-08-24' THEN t.PRESENT ELSE NULL END AS `2017-08-24`
FROM (
select e.employee_code,
Cast(e_l.time_in As date) As Day,
Case
WHEN e_l.time_in IS NULL THEN 'A'
WHEN DAYOFWEEK(e_l.time_in) In(7, 1) Then 'W'
ELSE 'P'
end as PRESENT
from employee As e
left join employees_logs As e_l on e.id = e_l.employee_id)
AS t
我尝试了这种雄辩的方式
public static function statusReport($data){
$start_date = $data['start_date'];
$end_date = $data['end_date'];
$date_array = self::getDatesFromRange($start_date, $end_date);
$query = DB::raw("(Select t.employee_code,
CASE WHEN t.day = '$start_date' THEN t.Present ELSE NULL END AS '$start_date')");
$query->addSelect(
DB::raw("(
SELECT employee.id as emp_id,
CONCAT(employee.firstname, " ",employee.lastname) AS employee_name,
CAST(employees_log.time_in as date) as date_given,
CASE WHEN employee_logs.time_in IS NULL THEN 'A'
WHEN leave.status_id = 4 AND leave_request.with_pay = 1 THEN 'L'
WHEN leave.status_id = 4 AND leave_request.with_pay = 0 THEN 'LOP'
WHEN DAYOFWEEK(employee_logs.time) In(7, 1) THEN 1 AS 'W'
ELSE 'P' END as status
LEFT JOIN employee_logs On employee.id = employees_logs.employee_id
JOIN leave On employee.id = leave.emp_id
JOIN leave_request On leave.id = leave_request.leave_id
WHERE employee_logs.time_in BETWEEN '$start_date' AND '$end_date') As `t`"));
$data = $query->get();
return $data;
}
预期输出是这个查询将告诉每天员工的状态。例如,如果这一天是周末,它将显示' W'在输出上。如果员工缺席' A'如果存在' P' 。我希望有人可以帮助我。提前致谢
答案 0 :(得分:0)
首先看一下head -1000 alert_PROD.log > alert_tmp ; cat alert_tmp > alert_PROD.log ; rm alert_tmp
方法:
addSelect
这只是通过与现有选定列合并来添加列。
要获得更多说明,请访问https://laravel.com/docs/5.4/queries#selects
现在我猜你会在不使用addSelect方法的情况下解决问题。
我的参考this