字段为空时的 Android 房间顺序

标签 android sqlite android-room

我用 房间持久性库 存储联系信息。
我想根据姓氏和名字检索联系人。

问题是没有姓氏和名字( 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/

相关文章:

android - 带有 Android 通用媒体播放器应用程序的 JSON(MediaPlayer 错误 1)

android - 如何在运行时删除危险权限?

android - Android EditText 是否具有带清除按钮的输入类型?

java - 如何仅在按下按钮时才重新加载 recyclerview?

python-3.x - sqlite3 python3,数据库的用户输入

android - 更新房间迁移的行

android - 如何通过自定义列表在房间中创建TypeConverter?

ios - 为什么要创建一个空数据库(0 口)?

ios - 在 Swift 中使用 FMDB 打开数据库

android - 如何将数据对象从 Room DB 传递到 WorkManager 类