2016-12-14 133 views
0

请有人帮我TI有两个表在我的MySQL数据库,MySQL查询打印考勤表

第一台 - (EMPLOYEE表)

EmployeeNo| EmployeeName 

第二个表 - (考勤表)

DATE | TIME | STATUS| EmployeeNo 

和保持仅EV日期的日历表的ERY月

我要生成类似于此 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; 
+0

它不是必须的,如果你知道任何其他方式使用存储过程等..或数据透视,请帮助我 –

+1

请参阅http://meta.stackoverflow.com/questions/333952/why-should-i-provide -an-mcve-for-what-seem-to-what-seems-a-very-simple-sql-query – Strawberry

+0

有没有其他办法可以实现这一点,我听说MySQL不支持pivoting,所以怎么可以我这样做, –

回答

-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 
+0

但这样,它会需要很多时间来执行 –

+0

我认为你可以使用缓存你的页面 –

+0

@ AbuMirza绝对的废话 - 尽管在这里需要一个OUTER JOIN(而不是INNER JOIN) – Strawberry