簡體   English   中英

如何返回首次出現的“行號”,SQL?

[英]How to return “row number” of first occurrence, SQL?

                         (row number not stored in db)

------------------------
|      |     A   |     | (1)
------------------------
|      |     B   |     | (2)
------------------------
|      |     C   |     | (3)
------------------------ -----Page 1
|      |     D   |     | (4)
------------------------
|      |     E   |     | (5)
------------------------
|      |     F   |     | (6)
------------------------ -----Page 2
|      |     F   |     | (7)
------------------------
|      |     F   |     | (8)
------------------------
|      |     G   |     | (9) -----Page 3

即搜索“ F”將返回6。

謝謝。

嘗試執行此操作的第一部分: 如何查詢SQL數據庫以返回包含指定數據的第一個“頁面”?

發現這些:
查詢中的順序行號

sqlite檢索sqlite中的行號

SELECT COUNT(*)
FROM tbl
WHERE letter < 'F'

(您將獲得數字減一,因此之后必須將其增加一)

試試這個,但是我不考慮表現

--create table
create table t(letter char(1))
go

--insert values
BEGIN TRAN
INSERT INTO t VALUES ('A')
INSERT INTO t VALUES ('B')
INSERT INTO t VALUES ('C')
INSERT INTO t VALUES ('D')
INSERT INTO t VALUES ('E')
INSERT INTO t VALUES ('F')
INSERT INTO t VALUES ('F')
INSERT INTO t VALUES ('F')
INSERT INTO t VALUES ('G')
COMMIT TRAN
GO

--and then try the select what you want
DECLARE @temp table(num int identity(1,1), letter char(1))

--use a temp table variable to store the data with row number
insert into @temp
SELECT * FROM t

--select the value
SELECT * FROM @temp WHERE letter = 'F'
GO

--drop the table finally
drop table t
GO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM