我用 房间持久性库 存储联系信息。
我想根据姓氏和名字检索联系人。
问题是没有姓氏和名字( null )的联系人在列表顶部收到。
如何查询以使它们位于列表的底部?
这是我的查询:
select * from CacheContact order by lastName asc,firstName asc LIMIT :count OFFSET :offset
CacheContact 对象:
@Entity
public class CacheContact {
private String expireDate;
@PrimaryKey
private long id;
private String firstName;
private long userId;
private String lastName;
private Boolean blocked;
private long creationDate;
private String profileImage;
@Embedded
private LinkedUser linkedUser;
private String cellphoneNumber;
private String email;
private String uniqueId;
private long notSeenDuration;
private boolean hasUser;
全道法:
@Query("select * from CacheContact order by lastName asc,firstName asc LIMIT :count OFFSET :offset")
List<CacheContact> getContacts(Integer count, Long offset);
最佳答案
试试这样:
select * from CacheContact order by lastName is null or lastName='', lastName, firstName is null or firstName='', firstName LIMIT :count OFFSET :offset
上面的查询还检查空字符串值。
来自 SQLite Release 3.30.0之后,您也可以使用 NULLS LAST ,因此您可以执行以下操作:
select * from table order by column_name NULLS LAST;
关于字段为空时的 Android 房间顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61483456/