views:

49

answers:

2

Sorted on column Y

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

User has option to enter wild card search, i.e. - "%5"

I would like to return to the user page 2 (as it has the first occurrence of something followed by 5.) OR find out how many rows there are before the column containing F5

(SQLite with C API)

+1  A: 

Assuming MySQL, resultset ordered by X and X is unique:

SELECT  COUNT(*)
FROM    mytable
WHERE   X <
        (
        SELECT  X
        FROM    mytable
        WHERE   y LIKE '%5'
        ORDER BY
                X
        LIMIT 1
        )
Quassnoi
@Quassnoi: Found one issue with this, any suggestions - http://stackoverflow.com/questions/3705238/how-do-i-get-like-and-count-to-return-the-number-of-rows-less-than-a-value-not-in
Tommy
+1  A: 

Assuming MSSQL, here's a roundabout way that kind of steps through the logic.. let me know if it doesn't help:

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
Fosco