我有一张表:
ref,type
1,red
2,red
3,green
4,blue
5,black
6,black
我希望 sqlite 查询的结果如下所示:
red,2
green,1
blue,1
black,2
我认为最困难的事情就是找到一个与我的问题相匹配的问题。而一旦找到,答案就在转角处等着我... :)
我在Google上快速搜索了"sqlite3计算唯一值",找到了这篇文章。但是,我想要计算所有唯一值的总数,而不是每个类别中有多少重复值。
从克里斯的结果表格中,我只想知道有多少个唯一颜色。正确答案是四[4]。
可以使用以下方法实现:select count(DISTINCT type) from table;
我快速谷歌了一下,找到了这个链接:http://www.mail-archive.com/sqlite-users@sqlite.org/msg38339.html
select type, count(type) from table group by type;
long count = DatabaseUtils.longForQuery(db, "select count(DISTINCT type) from table", null);
。 - ban-geoengineeringtotal
,像这样:select count(distinct type) as total from table
。 - ABCD.ca