2013-03-07 57 views
0

此查询不会返回我所期望的。为什么?Android SQLite3异常与DISTINCT?

表:

create table t 
(
    comment varchar(10), 
    id int 
); 

数据:

insert into t values ('C1', 1); 
insert into t values ('C2', 2); 
insert into t values ('C3', 3); 
insert into t values ('C4', 4); 
insert into t values ('C5', 5); 
insert into t values ('C6', 6); 
insert into t values ('C7', 7); 
insert into t values ('C8', 8); 
insert into t values ('C9', 9); 
insert into t values ('C1', 10); 

查询:

select distinct comment from t order by id desc limit 8; 

其结果是:

C9 
C8 
C7 
C6 
C5 
C4 
C3 
C2 

当我离开DISTINCT时,我得到最后一行,但我想抑制重复。 我的解决方法返回正确的结果集:

select comment from t group by comment order by max(id) desc limit 8; 

不过我很好奇,如果我的查询是无效的(T-SQL的例子并不在一个查询中同时允许一个DISTINCT和ORDER BY,除非该命令列是选择列表)?

回答

0

我想,只是猜测,C1是出于选择因为它有一个愚蠢,我猜它SQL - 选择这个愚蠢的ID为10而不是1.这就是为什么你得到2至9的IDS(完全8就像你从SQL中通过限制8所要求的那样)。请检查我的理论是否属实。我猜它的工作流程如同ID为1的记录,确定没有错误,接下来,id2,没有错过,确定...,编号为10 - 是编号为1的编号,编号为1的编号删除,编号为10的记录...

+0

不是一个真正的答案,但我会接受它作为唯一的答案。 – cdonner 2014-05-22 15:57:11