我正在尝试在我的sqlite3数据库中返回字段funny
中前10个值的记录。我使用了以下查询:
SELECT * FROM ngrams
ORDER BY funny DESC
LIMIT 10;
这将返回由funny
字段按降序排序的10条记录。然而,结果似乎只是前十个记录,其中有趣的是< 10 即可。这不是我想要的,我不确定我是怎么弄错的。为了记录,下面的查询返回~3,700,所以这些肯定不是我想要的十大记录。
SELECT Count(*) FROM ngrams
WHERE funny > 10;
如何返回前十个funny
值的记录?
列名称为ngram
,funny
,useful
,cool
,ngcount
。
输出:
sqlite> SELECT * FROM ngrams ORDER BY funny DESC LIMIT 10;
jetta| 9.914530| 5.726496| 6.367521| 117
emblem| 9.900000| 3.800000| 4.300000| 10
bt's| 9.875000| 11.375000| 10.625000| 8
dear phoenix| 9.857143| 8.285714| 7.857143| 7
look alikes| 9.857143| 7.428571| 6.428571| 7
alikes| 9.857143| 7.428571| 6.428571| 7
years im| 9.833333| 9.500000| 10.000000| 6
waaa| 9.833333| 8.000000| 7.000000| 6
earth do| 9.833333| 5.833333| 6.166667| 6
still full i| 9.833333| 7.500000| 8.000000| 6
sqlite> SELECT Count(*) FROM ngrams WHERE funny > 10;
3718
sqlite> SELECT * FROM ngrams WHERE ngram = 'megaphone';
megaphone| 14.777778| 10.555556| 10.555556| 9
sqlite> SELECT * FROM (
...> SELECT * FROM ngrams
...> ORDER BY funny DESC
...> ) topfunny LIMIT 10;
jetta| 9.914530| 5.726496| 6.367521| 117
emblem| 9.900000| 3.800000| 4.300000| 10
bt's| 9.875000| 11.375000| 10.625000| 8
dear phoenix| 9.857143| 8.285714| 7.857143| 7
look alikes| 9.857143| 7.428571| 6.428571| 7
alikes| 9.857143| 7.428571| 6.428571| 7
years im| 9.833333| 9.500000| 10.000000| 6
waaa| 9.833333| 8.000000| 7.000000| 6
earth do| 9.833333| 5.833333| 6.166667| 6
still full i| 9.833333| 7.500000| 8.000000| 6
sqlite>
答案 0 :(得分:2)
您的funny
列是TEXT。鉴于此:
create table ngrams (ngram text, funny text);
insert into ngrams (ngram, funny) values ('jetta', '9.914530');
insert into ngrams (ngram, funny) values ('emblem', '9.900000');
insert into ngrams (ngram, funny) values ('bt''s', '9.875000');
insert into ngrams (ngram, funny) values ('dear phoenix', '9.857143');
insert into ngrams (ngram, funny) values ('look alikes', '9.857143');
insert into ngrams (ngram, funny) values ('alikes', '9.857143');
insert into ngrams (ngram, funny) values ('years im', '9.833333');
insert into ngrams (ngram, funny) values ('waaa', '9.833333');
insert into ngrams (ngram, funny) values ('earth do', '9.833333');
insert into ngrams (ngram, funny) values ('still full i', '9.833333');
insert into ngrams (ngram, funny) values ('megaphone', '14.777778');
我得到了这个输出(为便于阅读而列化):
sqlite> select * from ngrams order by funny desc;
ngram |funny
jetta |9.914530
emblem |9.900000
bt's |9.875000
dear phoenix|9.857143
look alikes |9.857143
alikes |9.857143
years im |9.833333
waaa |9.833333
earth do |9.833333
still full i|9.833333
megaphone |14.777778
但如果funny
值是数字:
create table ngrams (ngram text, funny real);
insert into ngrams (ngram, funny) values ('jetta', 9.914530);
insert into ngrams (ngram, funny) values ('emblem', 9.900000);
insert into ngrams (ngram, funny) values ('bt''s', 9.875000);
insert into ngrams (ngram, funny) values ('dear phoenix', 9.857143);
insert into ngrams (ngram, funny) values ('look alikes', 9.857143);
insert into ngrams (ngram, funny) values ('alikes', 9.857143);
insert into ngrams (ngram, funny) values ('years im', 9.833333);
insert into ngrams (ngram, funny) values ('waaa', 9.833333);
insert into ngrams (ngram, funny) values ('earth do', 9.833333);
insert into ngrams (ngram, funny) values ('still full i', 9.833333);
insert into ngrams (ngram, funny) values ('megaphone', 14.777778);
然后输出就是你所期望的:
sqlite> select * from ngrams order by funny desc;
ngram |funny
megaphone |14.777778
jetta |9.91453
emblem |9.9
bt's |9.875
dear phoenix|9.857143
look alikes |9.857143
alikes |9.857143
years im |9.833333
waaa |9.833333
earth do |9.833333
still full i|9.833333
SQLite的松散类型系统既是一种祝福又是一种诅咒,你必须非常小心地确保你和SQLite就什么类型(在架构和你使用的值中)达成一致。由于SQLite没有固定的精度numeric
或decimal
类型,因此输出中的尾随零是一个死的,因为某些东西已经出现了。
答案 1 :(得分:0)
试试这个:
SELECT * FROM (
SELECT * FROM ngrams
ORDER BY funny DESC
) topfunny LIMIT 10;