我想查询一个表格,根据每个用户的位置(具有动态值)显示总付款+总体总额如下:
我能够了解到突破位置点,我唯一得不到的是Total列,这是我的SQL:
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
如上所述,如果我删除有问题的属性(总计),命令工作正常,但当我尝试获取总数时,我得到以下错误:
USE fypdb;
SET @sql = NULL;
SELECT GROUP_CONCAT(
DISTINCT CONCAT('SUM(CASE WHEN attendance.location_id=''', attendance.location_id,''' then
FORMAT(position.pay_rate*TIME_TO_SEC(TIMEDIFF(attendance.logout_time, attendance.login_time))/3600, 2) else 0 end) AS `',
location.name,'`')
) INTO @sql FROM attendance JOIN location ON attendance.location_id = location.location_id;
SET @sql = CONCAT('SELECT user.user_name,', @sql, '
SUM(FORMAT(position.pay_rate*TIME_TO_SEC(TIMEDIFF(attendance.logout_time, attendance.login_time))/3600, 2)) AS Total ', #this is the problematic attribute
'FROM attendance
JOIN user ON attendance.user_id = user.user_id
JOIN position ON user.position_id = position.position_id
GROUP BY attendance.user_id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
谁能告诉我我做错了什么?非常感谢你!
注意:我使用的是MySQL 5.7.9版本
答案 0 :(得分:0)
因为我可以看到你使用连接,并且在有问题的行中Total之后你缺少空格。下一行&#39; FROM没有空格,因此产生的查询是
AS TotalFROM
代替AS Total FROM