我需要在查询中连接两列并进行比较。这是我的代码。它不起作用。我想搜索全名。如何做到这一点:
@Query("SELECT * FROM shohada_table WHERE name LIKE :searchKey OR family LIKE :searchKey OR (SELECT name || family FROM shohada_table AS fullname ) like :orgSearch ORDER BY family ASC, name ASC")
fun getInfo( orgSearch: String): DataSource.Factory<Int, ShohadaItem>
在这个查询中,我以%SearchWord%"
的形式发送searchKey
,而orgSearch
将是原始单词:SearchWord
。例如,如果我想搜索Linus Torvalds
,则searchKey
将是%Linus Torvalds%
,而orgSearch
将是Linus Torvalds
。
嗨,感谢您的回复。我在下面的查询中得到了我想要的结果。
@Query("SELECT * FROM shohada_table WHERE name LIKE :searchKey OR family LIKE :searchKey OR (SELECT name || ' ' || family as fullname ) LIKE :searchKey ORDER BY position ASC, family ASC, name ASC")
fun getSearchedItems(searchKey: String): DataSource.Factory<Int, ShohadaItem>
如果我的搜索词是mahdi noori
,那么我的searcKey将是%mahdi%noori%
。我使用以下代码:
shohadaDao.getSearchedItems("%${searchKey.replace(" ", "%")}%")
您只需要使用名称||家族
@Query("SELECT * FROM shohada_table WHERE name LIKE :searchKey OR family LIKE :searchKey OR (name || family) :orgSearch ORDER BY family ASC, name ASC")
fun getInfo( orgSearch: String): DataSource.Factory<Int, ShohadaItem>