如何调用循环别名

时间:2017-12-07 07:49:12

标签: php

如何调用别名我已经循环的所有列?听起来很奇怪吧?

让它变得易于理解,这是我的代码:

    $begin = new DateTime( $start_date );
    $end = new DateTime( $end_date );
    $end = $end->modify( '+1 day' );

    $interval = new DateInterval('P1D');
    $daterange = new DatePeriod($begin, $interval ,$end);

    $date_array = array();
    $date_concat = array();

    foreach($daterange as $date){
        // $gc .= "GROUP_CONCAT(if(DATE(gen_access_control_log.created_at) = '".$date->format("Y-m-d")."', gen_access_control_log.created_at, NULL)) AS  'day".$date->format("Y-m-d")."',";

        $gc .= "CASE WHEN EXTRACT(DAY FROM gen_access_control_log.created_at) = '".$date->format("d")."' THEN 'P' ELSE 'A' END as 'days".$date->format('d')."',";

        $date_array[] = $date->format("M d");
        $date_concat[] = $date->format("Y-m-d");
    }

    $gc = rtrim($gc,',');

    // Fetch Students
    $students = DB::select(
        DB::raw("
            SELECT sys_user.*, sys_user_student.*, gen_access_control_log.*, sys_mf_grade.*, sys_mf_section.*,
            $gc
            FROM sys_user
            LEFT JOIN sys_user_student on sys_user.user_id = sys_user_student.user_id
            LEFT JOIN sys_mf_section on sys_user_student.section_id = sys_mf_section.section_id
            LEFT JOIN sys_mf_grade on sys_user_student.grade_id = sys_mf_grade.grade_id
            LEFT JOIN gen_access_control_log on sys_user_student.user_id = gen_access_control_log.user_id
            WHERE 
            Date(gen_access_control_log.created_at) BETWEEN '$search_from' AND '$search_to'
            AND sys_mf_section.section_id = ".$section_name."
            AND sys_mf_grade.grade_id = ".$grade_name."
            AND gen_access_control_log.status = 'entrance'
            GROUP BY gen_access_control_log.user_id             
        ")
    );

如您所见,我设置了别名day".$date->format("d")." 我的问题是,如何将其称为foreach循环

        foreach ($students as $key => $value) {
            $id = $value->identification_number;
            $fullname = $value->last_name.", ".$value->first_name;
            $gender = $value->gender;
        }

是否可以连接表格列?这样的事情 $logs = $value->day.$date->format("d")?如果没有,还有其他方法或方法吗?谢谢你们!

1 个答案:

答案 0 :(得分:0)

你有没有尝试过你的方法?如果您要将EXTRACT(DAY FROM gen_access_control_log.created_at)选择为新的别名extractedDay,那么选择的别名将为'days' . $value->extractedDay,不是吗?