让我们考虑以下SQlite示例表,其中存储了相同记录的多个版本:
CREATE TABLE MyRecords(
id LONG NOT NULL,
version LONG NOT NULL,
text TEXT,
PRIMARY KEY (id ASC, version ASC)
);
如何使用id
列仅为每个不同的version
值获取一行,以选择一个(如果表中保存了多个version
值,则com.venework.app.venework E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.venework.app.venework, PID: 3556
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.venework.app.venework/com.venework.app.venework.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.venework.app.venework.MainActivity" on path: DexPathList[[zip file "/data/app/com.venework.app.venework-1/base.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_9_apk.apk"],
nativeLibraryDirectories=[/data/app/com.venework.app.venework-1/lib/arm64, /system/lib64, /vendor/lib64]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2844)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.venework.app.venework.MainActivity" on path: DexPathList[[zip file "/data/app/com.venework.app.venework-1/base.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.venework.app.venework-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.venework.app.venework-1/lib/arm64, /system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
值最高?
答案 0 :(得分:0)
尝试此查询:
SELECT *
FROM MyRecords r1
WHERE version = (SELECT MAX(version) FROM MyRecords r2 WHERE r1.id = r2.id)
答案 1 :(得分:0)
您可以获取每个ID的最大版本,然后联接回来获取文本值
SELECT *
FROM (
SELECT id, MAX(version) version
FROM MyRecords
GROUP BY id
) q JOIN MyRecords r
ON q.id = r.id
AND q.version = r.version
答案 2 :(得分:0)
在SQLite 3.7.11或更高版本中,您只需use max() to select which row in a group to return:
SELECT id,
MAX(version) AS version,
text
FROM MyRecords
GROUP BY id;