SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('sum(case when week = ',week,
'then totaltime else 00 end) AS `',
week, '`')
)INTO @sql
FROM mytable;
SET @sql = CONCAT('SELECT TRIM(UPPER(mytable.empcode))AS CODE,
TRIM(UPPER(mytable.empname)) EMPNAME,
', @sql, '
from mytable
group by empcode
order by empname ');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
我在这里使用数据透视表,想根据查询动态生成的周字段来计算总时间的总和,但是我收到了错误:
"#1064 - 您的SQL语法出错;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 通过empcode或'来自mytable group附近的'在第6行"