SQL 除了按描述顺序排列的前 10 条记录
我想提取语句中除前 10 条之外的所有记录,但在子查询“关键字 ORDER 附近的语法不正确”中使用 ORDER BY 时,我不断遇到问题
@ID INT
as
SELECT ComVID, VID, MID, Ucomment, UdateAdded, MemberId, UserName, Avatar
FROM table.miComments JOIN table.mbrProfile2 ON MID = MemberId
WHERE VID = @ID EXCEPT (SELECT ComVID, VID, MID, Ucomment, UdateAdded, MemberId, UserName, Avatar FROM table.miComments JOIN table.mbrProfile2 ON MID = MemberId
WHERE VID = @ID ORDER BY UdateAdded DESC) 'ERROR: Incorrect Syntax near the keyword ORDER'
ORDER BY UdateAdded DESC
I want to pull all records except the TOP 10 in my statement but I keep running into problems when using ORDER BY in my subquery "Incorrect Syntax near the keyword ORDER"
@ID INT
as
SELECT ComVID, VID, MID, Ucomment, UdateAdded, MemberId, UserName, Avatar
FROM table.miComments JOIN table.mbrProfile2 ON MID = MemberId
WHERE VID = @ID EXCEPT (SELECT ComVID, VID, MID, Ucomment, UdateAdded, MemberId, UserName, Avatar FROM table.miComments JOIN table.mbrProfile2 ON MID = MemberId
WHERE VID = @ID ORDER BY UdateAdded DESC) 'ERROR: Incorrect Syntax near the keyword ORDER'
ORDER BY UdateAdded DESC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用的是 MS SQL Server,则没有 LIMIT 或 OFFSET 等效项,因此您必须使用子查询来完成您想要的操作。
以下 StackOverflow 链接包含更多信息,可能会有所帮助:
LIMIT 10..20在 SQL Server 中
If you're using MS SQL Server, there is no LIMIT or OFFSET equivalent, so you'd have to use a subquery to accomplish what you want.
The following StackOverflow link has a lot more information in it, which may be helpful:
LIMIT 10..20 in SQL Server
您正在谈论偏移。如果您有一个返回行
1,2,3,4,5,6,7
的查询,并且您想跳过前 3 行(产生4,5,6,7
code>),您可以指定偏移量 3。在 MySQL 中,您可以使用接受偏移量参数的 LIMIT 子句。在 PostgreSQL 中,您需要 OFFSET 子句。 SQLServer(截至我上次被迫使用它)不支持偏移量。
PostgreSQL
MySQL
在 MySQL 中,如果不指定偏移量,就无法指定偏移量。由于某些奇怪的原因而受到限制:
You're talking about an offset. If you have a query returning rows
1,2,3,4,5,6,7
and you want to skip over the first 3 (yielding4,5,6,7
), you can specify an offset of 3.In MySQL you can use the LIMIT clause which accepts an offset argument. In PostgreSQL you'll want the OFFSET clause. SQLServer (as of the last time I was forced to use it) doesn't support offsets.
PostgreSQL
MySQL
In MySQL you cannot specify an offset without also specifying a limit for some bizarre reason:
我目前无法重现您确切的 SQL 配置,但要回答“一般问题”:
示例:
假设我们有一个人员及其姓名 + 的数据库地址组合将它们限定为唯一记录(不是在关系级别上,而是在业务需求级别上)。
EXCEPT
自 SQL Server 2005 起可用。I can't reproduce your exact SQL configuration at this moment, but to answer the "general question":
Example:
Assuming we have a database of people and their Name + Address combination qualifies them as a unique record (not on a relational level, but on a business requirements level).
EXCEPT
has been available since SQL Server 2005.您可以将其用作简单的查询:
You can use this as a simple query :