【问题标题】:How to select and then split data then select the second data?如何选择然后拆分数据然后选择第二个数据?
【发布时间】:2019-02-28 14:28:31
【问题描述】:

所以我选择前 60 个数据,然后我想要删除前 30 个数据以获得第 31 个数据。但我正在努力寻找任何解决方案。这是我的代码:

    SqlDataAdapter daa = new SqlDataAdapter("SELECT TOP 60  Name, Signature 
    FROM TBL_Attendance WHERE Status = '" + lblStat.Text + "' AND Date = '" + 
    this.dtSelectDate.Text + "' ", conn);

这是我要编辑以获取第 31 条数据的查询。

我使用的是 SQL Server 2008。

【问题讨论】:

  • 查看添加行号:docs.microsoft.com/en-us/sql/t-sql/functions/… 然后您可以将其用作子查询并选择您想要的行号(所以其中 RowNumber = 31)。
  • 所以基本上你只想要从第 31 个 id 开始的数据?​​
  • 您每次都会得到随机结果。您必须在 SQL Server 中使用 orderby 才能每次都获得相同的结果。 SQL在搜索时使用多线程,并且顺序是随机的。
  • 我想选择第 31 个值而不是 id,因为我正在使用它来生成报告
  • 你能从数据表中读取第 31 行吗?

标签: c# sql vb.net


【解决方案1】:

根据您的 SQL 数据库,您可能希望查看以下内容:

https://docs.microsoft.com/en-us/previous-versions/sql/compact/sql-server-compact-4.0/gg699618(v=sql.110)

*这仅适用于 Microsoft SQL DB 2012 >

它像分页一样工作。

【讨论】:

  • 我使用的是 Sql Server 2008
【解决方案2】:

试试这里的建议:https://www.codeproject.com/Questions/219741/Select-nth-row-of-a-table-in-sql-server

WITH whateverhere AS (
    SELECT (ROW_NUMBER() OVER (ORDER BY TBL_Attendance.Name)) as row,*
    FROM TBL_Attendance)
SELECT * FROM whateverhere WHERE row > 31

【讨论】:

  • 它给了我错误,找不到行列。我正在使用此查询为我的数据集选择值并将其放入我的 RDLC 报告中。
  • 您可以尝试在您的 SQL 服务器中运行此查询吗?看看你是否仍然得到那个错误。
【解决方案3】:

这对我有用。我只是将图像更改为 varbinary 以进行比较

     SqlDataAdapter daa = new SqlDataAdapter("SELECT Name, Signature FROM 
     TBL_Attendance WHERE Status = '" + lblStat.Text + "' AND Date = '" + 
     this.dtSelectDate.Text + "' EXCEPT SELECT TOP 30 Name, Signature FROM 
     TBL_Attendance  WHERE Status = '" + lblStat.Text + "' AND Date = '" + 
     this.dtSelectDate.Text + "' ", conn);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多