这是我的SQL查询,但出现错误:
关键字“ order”附近的语法不正确
我已经检查了所有内容,但无法纠正。请帮助
SELECT TOP (1)
CONVERT(VARCHAR(19), DayCheckOut, 120)
FROM
(SELECT TOP (2)
A1.DayCheckOut AS DayCheckOut
FROM
EmployeeAttendace A1
INNER JOIN
EmployeeMaster B1 ON A1.EmployeeId = B1.Id
WHERE
B1.EmailId = 'raja.xyz@gmail.com'
ORDER BY
A1.Id DESC)
ORDER BY
DayCheckOut DESC
答案 0 :(得分:3)
提供别名
SELECT top 1
convert(varchar(19),DayCheckOut,120)
FROM
(SELECT top 2 A1.DayCheckOut as DayCheckOut FROM EmployeeAttendace A1 INNER JOIN
EmployeeMaster B1 ON A1.EmployeeId = B1.Id WHERE B1.EmailId =
'pooja.yadav@computronics.in' order by A1.Id desc) as AliasName
order by DayCheckOut desc
答案 1 :(得分:2)
您需要一个别名,例如q
用于子查询(SELECT top 2 A1.DayCheckOut as DayC ... order by A1.Id desc) q
对于 MS SQL Server DB 这是非常常见的问题。
答案 2 :(得分:2)
不需要该子查询,现代SQL Server版本支持OFFSET
/ FETCH FIRST
。
SELECT convert(varchar(19),A1.DayCheckOut,120)
FROM EmployeeAttendace A1 INNER JOIN EmployeeMaster B1 ON A1.EmployeeId = B1.Id
WHERE B1.EmailId = 'raja.xyz@gmail.com'
order by A1.Id desc
offset 1 fetch next 1 row only
即使用OFFSET
跳过第一行,使用FETCH NEXT
仅返回第一行。
答案 3 :(得分:0)
它在那里出错,因为您需要一个子查询别名。
但是,您不需要冗余子查询:
SELECT top (1) convert(varchar(19),A1.DayCheckOut,120) as DayCheckOut
FROM EmployeeAttendace A1
INNER JOIN EmployeeMaster B1 ON A1.EmployeeId = B1.Id
WHERE B1.EmailId = 'raja.xyz@gmail.com'
order by A1.DayCheckOut desc;
答案 4 :(得分:0)
选择顶部(1)转换(varchar(19),DayCheckOut,120)
FROM(选择顶部(2)A1.DayCheckOut作为DayCheckOut
来自员工A1内部联接
员工大师B1
开启A1.EmployeeId = B1.Id
('raja.xyz@gmail.com')中的B1.EmailId在哪里
ORDER BY A1.Id desc
)
按DayCheckOut DESC排序