Mysql查询打印考勤表

时间:2016-12-14 11:30:08

标签: mysql sql

请有人帮助我,我的MYSQL数据库中有两个表,

第一个表 - (员工表)

组成
EmployeeNo| EmployeeName

第二个表 - (出勤表)

组成
DATE | TIME | STATUS| EmployeeNo

以及仅保存每月日期的日历表

我想生成类似于此的考勤表 Click here

我最终编写了以下SQL,但它给了我语法错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your 
        MySQL server version for the right syntax to use near 'from (select cal.calendarDate, 
        emp.first_name,emp.nu' at line 2

和我的查询,

    SET @sql = NULL;
    SELECT
    GROUP_CONCAT(DISTINCT
    CONCAT(
      'max(CASE WHEN calemp.calendarDate = ''',date_format(calendarDate, '%Y-%m-%d'),''' THEN coalesce(att.inorout, ''P'') END) AS `',date_format(calendarDate, '%Y-%m-%d'), '`'
    )
      ) INTO @sql
     FROM yearly_date_calendar
     where calendarDate >= '2016-11-01'
     and calendarDate <= '2016-11-30';

    SET @sql = CONCAT('SELECT calemp.first_name,calemp.nurse_code,',@sql,'
            from
            (
              select cal.calendarDate,emp.first_name,emp.nurse_code
              from yearly_date_calendar cal
              cross join syscare_caregiver emp
            ) calemp
            left join syscare_employee_attendance att
              on calemp.nurse_code = att.emp_code
              and calemp.calendarDate = att.attendance_date
            where calemp.calendarDate>=''2016-11-01''
              and calemp.calendarDate <= ''2016-11-30''
            group by calemp.first_name, calemp.nurse_code,calemp.calendarDate
      ');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

1 个答案:

答案 0 :(得分:-1)

您可以使用以下查询并获取所有数据库结果

select EmployeeTable.EmployeeNo,Employee Table.EmployeeName,Attendance table.DATE,Attendance table.TIME,Attendance table.STAUS,Attendance table.EmployeeNo from Employee Table INNER JOIN Attendance table ON 
EmployeeTable.EmployeeNo=Attendance table.EmployeeNo