ORMLite SELECT DISTINCT场 [英] ORMLite Select Distinct Fields

查看:876
本文介绍了ORMLite SELECT DISTINCT场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQLite表(在Android),有许多领域,但某些领域的重复/非规范化。我想选择一组该数据显着,并把它们作为实际的对象。

示例

书表

 标题摘要作者
小约翰尼一个有趣的孩子约翰尼自己
大约翰尼一个有趣的成人约翰尼自己
 

我想从这个列表(约翰尼自己)提取一个作者,希望我能够与Java做手工这与ORMLite代替。

解决方案
  

我想选择一组该数据显着,并把它们作为实际的对象。

我不知道100%,我在这里了解你的查询,但 ORMLite 支持<一个href="http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/stmt/QueryBuilder.html#distinct%28%29"><$c$c>distinct()在方法的的QueryBuilder 应该做你想要什么。所以,你的code看起来是这样的:

 名单,其中,图书&GT;结果= booksDao.queryBuilder()
    。.distinct()selectColumns(作家)查询()。
 

在这种情况下,所产生的图书的对象只会有作者字段集,而不是 ID 字段或其他任何东西。如果你只是想作者姓名,而不是对象,那么你可以这样做:

  GenericRawResults&LT;的String []&GT; rawResults =
    booksDao.queryRaw(SELECT DISTINCT笔者从书);
对于(字符串[] resultColumns:rawResults){
    字符串笔者= resultColumns [0];
    ...
}
 

I have a SQLite table (on Android) that has numerous fields, but certain fields are repeated/denormalized. I would like to select a distinct set of this data and use them as actual objects.

Example

books table

title            summary        author
Little Johnny    A funny kid    Johnny Himself
Big Johnny       A funny adult  Johnny Himself

I would like to extract one author from this list ("Johnny Himself") and would expect I should be able to do this with ORMLite instead of manually with Java.

解决方案

I would like to select a distinct set of this data and use them as actual objects.

I'm not 100% sure I understand your query here but ORMLite supports a distinct() method on the QueryBuilder that should do what you want. So your code would look something like:

List<Book> results = booksDao.queryBuilder()
    .distinct().selectColumns("author").query();

In this case, the resulting Book objects would only have the author field set and not the id field or anything else. If you just wanted the author names instead of objects then you could do:

GenericRawResults<String[]> rawResults =
    booksDao.queryRaw("SELECT DISTINCT author FROM books");
for (String[] resultColumns : rawResults) {
    String author = resultColumns[0];
    ...
}

这篇关于ORMLite SELECT DISTINCT场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆