如何在第一次出现LIKE之前找到列数?

时间:2010-09-10 18:56:17

标签: sql database database-design sqlite

在Y列上排序

   X       Y        Z 
------------------------
|      |     A1  |     |
------------------------
|      |     B2  |     |
------------------------
|      |     C3  |     |
------------------------ -----Page 1
|      |     D3  |     |
------------------------
|      |     E4  |     |
------------------------
|      |     F5  |     |
------------------------ -----Page 2
|      |     G5  |     |
------------------------
|      |     F6  |     |
------------------------
|      |     G7  |     | -----Page 3  

用户可以选择输入外卡搜索,即 - “%5

我想返回用户页面2(因为它首次出现的是后跟5的内容。)或者找出包含F5的列之前有多少行

(带有C API的SQLite)

2 个答案:

答案 0 :(得分:1)

假设MySQLXX排序的结果集是唯一的:

SELECT  COUNT(*)
FROM    mytable
WHERE   X <
        (
        SELECT  X
        FROM    mytable
        WHERE   y LIKE '%5'
        ORDER BY
                X
        LIMIT 1
        )

答案 1 :(得分:1)

假设MSSQL,这是一种迂回的方式,通过逻辑的步骤..让我知道它是否没有帮助:

declare @perPage int
declare @searchString varchar(20)
declare @countBefore int
declare @firstMatch varchar(20)
declare @resultPage int

set @perPage = 3
set @searchString = '%5'

select @firstMatch = (select top 1 y from myTable where y like @searchString order by y)
select @countBefore = (select count(*) from myTable where y < @firstMatch)
select @resultPage = (@countBefore / @perPage) + 1