c# - 带限制的 SQL 查询

标签 c# sql sql-server

我在 SQL Server 中有这个表:

ID   | videoid  | title
=========================
1    | id1      | title1
2    | id2      | title2
3    | id3      | title3

我想创建在标题行中搜索的选择方法:

SELECT * FROM [movie].[dbo].[movies] 
WHERE title like '%' + '%s' + '%'

我在 MySQL 中寻找类似 Limit 的东西,从 SELECT 结果中我将能够得到 0-20,21-40,41-60 结果。

对这个查询有帮助吗?

我尝试使用 LIMIT 0, 10 并收到此错误:

Could not find stored procedure 'LIMIT'.

最佳答案

您需要将 TOP N 与 SQL SERVER 一起使用。

SELECT TOP 10 * FROM [movie].[dbo].[movies] 
WHERE title like '%' + '%s' + '%'
ORDER BY SomeColumn -- Specify your column for ordering

参见:TOP (Transact-SQL)

Limits the rows returned in a query result set to a specified number of rows or percentage of rows in SQL Server

另请参阅 Best Practices in docs .

In a SELECT statement, always use an ORDER BY clause with the TOP clause. This is the only way to predictably indicate which rows are affected by TOP.

如果您正在寻找寻呼记录,那么您将需要 ROW_NUMBER

关于c# - 带限制的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22455057/

相关文章:

sql - 无效的对象名称 sql

c# - 通过设置属性值创建新对象的 2 种方法

c# - 带有列表的 asp.net 核心 ioptions

c# - 如何获取使用 facebook 注册应用程序注册我的网站的 facebook 用户的个人资料照片(不使用 mvc)

sql - 按人查找最后一条记录

mysql - 选择不同的 2 列

c# - ASP.Net Core 如何在 EF Core 和 Identity 中获取用户角色

c# - 从组合框 C# 表单发送文本作为 SQL 过程的输入

sql-server - SQL Server 是否支持分片?

sql-server - 从 SQL Server 的子查询中选择多列