2010-09-10 16 views
1

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

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

用户有选项输入通配符搜索,即 - “%5

我想返回给用户2页(因为它的东西第一次出现接着5.)OR找出有多少行是包含塔之前F5

(SQLite的与C API)

回答

1

假设MySQL,结果集通过X订购和X是独一无二的:

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

发现一个问题与此,任何建议 - http://stackoverflow.com/questions/3705238/how-do-i-get-like-and-count-to-return-the-number-of-rows-less-thana-a-价值未在 – 2010-09-13 23:57:31

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 
相关问题