请有人帮我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;
它不是必须的,如果你知道任何其他方式使用存储过程等..或数据透视,请帮助我 –
请参阅http://meta.stackoverflow.com/questions/333952/why-should-i-provide -an-mcve-for-what-seem-to-what-seems-a-very-simple-sql-query – Strawberry
有没有其他办法可以实现这一点,我听说MySQL不支持pivoting,所以怎么可以我这样做, –