简体   繁体   English

sqlite rawquery无法正常工作

[英]sqlite rawquery not working correctly

I have two table their names are profile and messages. 我有两个表,它们的名称分别是个人资料和消息。 I try to get profile data and last message which related by profileId from sql. 我尝试从SQL获取与profileId相关的配置文件数据和最后一条消息。

profiles 型材

  • profileId(INTEGER) profileId(INTEGER)
  • profileName(TEXT) profileName(TEXT)
  • profileImgPath(TEXT) profileImgPath(TEXT)

messages 讯息

  • messageId (INTEGER) messageId(INTEGER)
  • profileId (INTEGER) profileId(INTEGER)
  • messageContent (INTEGER) messageContent(INTEGER)
  • messageType (INTEGER) messageType(INTEGER)
  • messageStatus(INTEGER) messageStatus(INTEGER)
  • messageTime(TEXT) messageTime(TEXT)

data examples 资料范例

表内容

在此处输入图片说明

my query 我的查询

SQLiteDatabase db = this.getReadableDatabase();


     String selectQuery = "select " +
     "profiles.profileId," +
     "profiles.profileName," +
     "profiles.profileImgPath," +
     "profiles.profileStatus," +
     "messages.messageContent," +
     "messages.messageTime " +
     "from profiles left join messages on profiles.profileId=messages.profileId " +
     "group by profiles.profileId " +
     "order by messages.messageId DESC;";
          Cursor c = db.rawQuery(selectQuery, null);
    Log.i("query",selectQuery);
    if (c.moveToFirst()) {
        do {

            Log.i("gokberksql", c.getString(1) + "x" + c.getString(4));
            } while (c.moveToNext());
    }

results 结果

gokberksql﹕ sinanxKanka naber

But need to return 但需要退货

gokberksql﹕ sinanxIyi senden naber

In addition this query working well in my computer editors but android results not correctly. 另外,此查询在我的计算机编辑器中运行良好,但android结果不正确。

You should select the max messageTime in your query. 您应该在查询中选择最大messageTime。 This will give you the values of columns in the same row as the one that contains the max. 这将为您提供与包含最大值的列在同一行中的列的值。

String selectQuery = "select " +
    "profiles.profileId," +
    "profiles.profileName," +
    "profiles.profileImgPath," +
    "profiles.profileStatus," +
    "messages.messageContent," +
    "max(messages.messageTime) as messageTime " + // <- here
    "from profiles " +
    "left join messages on profiles.profileId = messages.profileId " +
    "group by profiles.profileId" +
    "order by profiles.profileId, messages.messageId DESC;";

Note that this behavior was introduced in SQLite version 3.7.11, which became standard in API 16 (JellyBean). 请注意,此行为是在SQLite 3.7.11版中引入的,该版本在API 16(JellyBean)中成为标准。 If you need to support API 15 or older, you may have to use a different query. 如果需要支持API 15或更早版本,则可能必须使用其他查询。

Why do you have a group by profiles.profileId, that is not needed in your case since you are not doing any aggregations over the profileId group. 为什么要有一个由profiles.profileId组成的组,在这种情况下就不需要了,因为您没有对profileId组进行任何聚合。 Try with 试试看

String selectQuery = "select " +
   "profiles.profileId," +
   "profiles.profileName," +
   "profiles.profileImgPath," +
   "profiles.profileStatus," +
   "messages.messageContent," +
   "messages.messageTime " +
 "from profiles left join messages on   profiles.profileId=messages.profileId " +
   "order by profiles.profileId, messages.messageId DESC;";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM