使用LIMIT关键字查询SQL Server语法错误



我正在尝试运行此查询,但我得到一个错误

关键字'limit'附近语法错误

SQL查询:

select * 
from messages as msg 
where "+"(SELECT date from messages where date between
msg.firsttime and msg.lasttime and receiver is null limit 1) 
limit 5

除了'limit'关键字,我的错误在哪里?

Select top 5 * 
from messages as msg 
where (
SELECT top 1 date 
from messages 
where date between msg.firsttime and msg.lasttime 
and receiver is null
) 

这里有两个问题

首先是LIMITLIMIT不是ansi标准SQL。它是MySQL和SQLite中使用的专有扩展。SQL Server对此有自己的专有扩展(SELECT TOP),但它也支持使用OFFSET/FETCH的实际标准。

接下来是WHERE子句。这个查询在主WHERE子句中有一个嵌套的SELECT语句来查找date的值…然后不与任何进行比较. 没有条件运算。您需要对该结果进行某种布尔比较。

最新更新