Mysql查询打印考勤表

Mysql查询打印考勤表,mysql,sql,Mysql,Sql,请有人帮我在我的MYSQL数据库中有两个表 第一个表(员工表)包括 EmployeeNo| EmployeeName 第二个表——(考勤表)包括 DATE | TIME | STATUS| EmployeeNo 还有一个日历表,只保存每个月的日期 我想生成与此类似的考勤表 我最终编写了以下SQL,但它给了我语法错误 #1064 - You have an error in your SQL syntax; check the manual that corresponds to your

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

第一个表(员工表)包括

EmployeeNo| EmployeeName
第二个表——(考勤表)包括

DATE | TIME | STATUS| EmployeeNo
还有一个日历表,只保存每个月的日期

我想生成与此类似的考勤表

我最终编写了以下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;
SET@sql=NULL;
挑选
组_CONCAT(不同
海螺(
“max(当calemp.calendarDate='',日期格式(calendarDate,%Y-%m-%d'),''然后合并(att.inorout,'P'')结束)为“”,日期格式(calendarDate,%Y-%m-%d'),”
)
)进入@sql
从年度日期日历开始
其中calendarDate>=“2016-11-01”

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

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

如果您知道使用存储过程等的任何其他方法,那么这不是必须的。。或者pivot,请帮助meSee是否有其他方法来实现这一点,我听说MySQL不支持pivot,那么我该怎么做呢?我认为您可能会将数据存储和检索与数据显示混淆,后者通常在应用程序代码中得到最好的解决(比如生成漂亮图片的代码).你刚才不是用不同的用户帐户问了同样的问题吗?为什么要重复这个问题?但是那样的话,执行起来会花很多时间。我想你可以使用你的缓存page@AbuMirza绝对是胡说八道-尽管这里需要外部连接(与内部连接相反)