在MySQL工作台中执行此SQL语句时遇到问题。谁能指出我的问题?
SELECT concat(LocationName,' - ', StationName) AS LocationStation, EmpName AS '0000-0100', EmpName AS '0100-0200', EmpName AS '0200-0300', EmpName AS '0300-0400', EmpName AS '0400-0500', EmpName AS '0500-0600', EmpName AS '0600-0700', EmpName AS '0700-0800', EmpName AS '0800-0900', EmpName AS '0900-1000', EmpName AS '1000-1100', EmpName AS '1100-1200', FROM satsschema.employeeslot
WHERE LocationName = 'T2 PML'
and AllocationDate = '10-Aug'
and (EmpTime >= '00:00:00' and EmpTime <= '12:00:00')
上面的状态提示我这个错误,“你的SQL语法有错误,请查看与你的MySQL服务器版本对应的手册,使用正确的语法来使用整齐的'FROM satsschema.employeeslot WHERE LocationName ='T2 PML'和AllocationDate ='在第1行。“
其次,除了输入所有内容之外,还有其他方法可以缩短这条线吗?
EmpName AS '0000-0100', EmpName AS '0100-0200', EmpName AS '0200-0300', EmpName AS '0300-0400', EmpName AS '0400-0500', EmpName AS '0500-0600', EmpName AS '0600-0700', EmpName AS '0700-0800', EmpName AS '0800-0900', EmpName AS '0900-1000', EmpName AS '1000-1100', EmpName AS '1100-1200', FROM satsschema.employeeslot
非常感谢帮助。
答案 0 :(得分:1)
删除FROM
之前的逗号。它会导致错误。
SELECT concat(LocationName,' - ', StationName) AS LocationStation, EmpName AS '0000-0100', EmpName AS '0100-0200', EmpName AS '0200-0300', EmpName AS '0300-0400', EmpName AS '0400-0500', EmpName AS '0500-0600', EmpName AS '0600-0700', EmpName AS '0700-0800', EmpName AS '0800-0900', EmpName AS '0900-1000', EmpName AS '1000-1100', EmpName AS '1100-1200' FROM satsschema.employeeslot
WHERE LocationName = 'T2 PML'
and AllocationDate = '10-Aug'
and (EmpTime >= '00:00:00' and EmpTime <= '12:00:00')
请注意,错误消息确切地说明错误在哪里。
在MySQL中,AS
关键字对于列别名是可选的,因此您可以用EmpName '0000-0100'
代替EmpName AS '0000-0100'
。我不认为有一个简短的解决方案可以为一个列提供多个别名,因为它通常不是必需的。
答案 1 :(得分:0)
在comma
子句之前删除额外的from
。
SELECT concat(LocationName,' - ', StationName) AS LocationStation,
EmpName AS '0000-0100',
EmpName AS '0100-0200',
EmpName AS '0200-0300',
EmpName AS '0300-0400',
EmpName AS '0400-0500',
EmpName AS '0500-0600',
EmpName AS '0600-0700',
EmpName AS '0700-0800',
EmpName AS '0800-0900',
EmpName AS '0900-1000',
EmpName AS '1000-1100',
EmpName AS '1100-1200' -- remove comma here
FROM satsschema.employeeslot
WHERE LocationName = 'T2 PML'
and AllocationDate = '10-Aug'
and (EmpTime >= '00:00:00' and EmpTime <= '12:00:00')