English 中文(简体)
在qlite结果之前选择行文
原标题:Selecting the row before the resultset in sqlite
  • 时间:2010-08-20 04:05:40
  •  标签:
  • sql
  • sqlite

因此,我要说的是,有一个表格叫做data

data

id | date      | name
---------------------    
1  | 19/8/2010 | John    
2  | 19/8/2010 | Mary    
3  | 20/8/2010 | Peter    
4  | 20/8/2010 | Bert     
5  | 20/8/2010 | Ernie 

如果是这样作的话,

SELECT * FROM data where date =  20/8/2010 ;

也可以把这一选择发言放在前面。 在此情况下,我回归第3-5号,但有可能以任何方式篡改声明,选择第2行?

最佳回答

改编@SteveCav s Solutions to UNION with the original query:

SELECT d.* 
FROM   data d
JOIN   (
          SELECT MIN(id) lowest_id
          FROM   data 
          WHERE  date =  20/8/2010 
       ) dt ON (dt.lowest_id - 1 = d.id)
UNION
SELECT * 
FROM   data 
WHERE  date =  20/8/2010 ;

测试案例(第3类测试):

CREATE TABLE data (id int, date date, name varchar(10));

INSERT INTO data VALUES (1,  19/8/2010 ,  John );
INSERT INTO data VALUES (2,  19/8/2010 ,  Mary );
INSERT INTO data VALUES (3,  20/8/2010 ,  Peter );
INSERT INTO data VALUES (4,  20/8/2010 ,  Bert );
INSERT INTO data VALUES (5,  20/8/2010 ,  Ernie );

结果:

id          date        name      
----------  ----------  ----------
2           19/8/2010   Mary      
3           20/8/2010   Peter     
4           20/8/2010   Bert      
5           20/8/2010   Ernie 

假设通过“前行”,你打算使用以前的<代码>id值。

问题回答

我不了解有关“公平贸易与发展”的任何内容,但对于服务器,例如,我可以做以下事情:

SELECT * FROM (SELECT TOP 1 * FROM data where date <  20/8/2010  ORDER BY date DESC, id DESC)
UNION
SELECT * FROM data where date =  20/8/2010 
;

这假设,你的“前行”标准是最长的行程;你被问到的日期,如果超过一行的日期,那么就充其量。

Select d.* 
from data d, 
(SELECT min(id) As LowestID FROM data where date =  20/8/2010 ) m
where d.id=m.LowestID-1
Select Id, [Date], Name
From MyTable
Where [Date] =  2010-08-20 
Union All
Select Id, [Date], Name
From MyTable
Where Id = (
            Select Max(T2.Id)
            From MyTable As T2
            Where T2.[Date] <  2010-08-20 
            )

也许这接近你们想要的:

 SELECT * FROM data where date <=  20/8/2010  order by date desc;

然后,在8月20日前的第一行之后,不再宣读结果。

一旦上述查询结果在目前清单中公布,你就可以与一位治疗师接触任何返回的牢房。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签