SugarOrm:OrderBy相关表

Luk*_*oft 7 android sugarorm

有了SugarORM,我理解我可以做到这一点

public class Book extends SugarRecord<Book> {
  String name;
  String ISBN;
  String title;
  String shortSummary;

  // defining a relationship
  Author author;
}
Run Code Online (Sandbox Code Playgroud)

那么我怎样才能在Book.class上找到我可以由作者订购的内容.

我试过了

Book.find(Book.class, null, null, null, "author.id desc",null);;
Run Code Online (Sandbox Code Playgroud)

Book.find(Book.class, null, null, null, "author desc",null);;
Run Code Online (Sandbox Code Playgroud)

而这些都不会奏效

小智 10

对于简单查询,建议使用查询构建器方法,例如:

Select.from(Book.class)
.orderBy("ISBN")
.list();
Run Code Online (Sandbox Code Playgroud)

无论如何,请记住,SugarORM基于SQLite,在您的特定情况下,您尝试通过关系进行排序.然后你应该建立一个带连接的自定义查询,并按表作者的字段顺序(id,name,surname等......取决于你的目的)

  • @SoriaGustavo如果你想指定asc/desc,只需将它放在orderBy中,例如`orderBy("ISBN desc")`. (8认同)

Dea*_*anK 3

我的建议是你使用 Sugar ORM Custom Query Builder,它可以通过使用这个来调用

Book.executeQuery("VACUUM");
Run Code Online (Sandbox Code Playgroud)

或者您也可以使用findWithQuery具有以下语法的方法

List<Note> notes = Note.findWithQuery(Note.class, "SELECT * FROM Book 
                                        ORDER BY author DESC", null);
Run Code Online (Sandbox Code Playgroud)