gpt4 book ai didi

java - 基于第一次排序,再次排序 Sqlite vs Java (Android)

转载 作者:行者123 更新时间:2023-12-02 11:37:16 25 4
gpt4 key购买 nike

组合多个Sqlite查询

数据库结构

CREATE TABLE `userdetails` ( `_id` INTEGER NOT NULL, `username` TEXT NOT NULL, `code` TEXT NOT NULL, `usertype` TEXT NOT NULL, `userID` TEXT NOT NULL, PRIMARY KEY(`_id`) )
_id   username code  usertype  userID  "1"   "user1"  "ERE" "type1"   "1"  "2"   "user2"  "ERE" "type2"   "2"  "3"   "user1"  "ERE" "type3"   "3"  "4"   "user1"  "ERE" "type4"   "4"  "5"   "user2"  "ERE" "type1"   "5"  "6"   "user2"  "ERE" "type2"   "6"  "7"   "user2"  "ERE" "type4"   "7"  "8"   "user2"  "ERE" "type3"   "8"  "9"   "user2"  "ERE" "type5"   "9"  "10"  "user2"  "ERE" "type1"   "10"  "11"  "user2"  "ERE" "type2"   "11"  

I'm writing this in SQLite Query in Android Application

output should beHighest userID value will have some user type, all that usertype should be there, followed by next highest value.... see example

"11" "user2" "ERE" "type2" "11""6"  "user2" "ERE" "type2" "6"      "2"  "user2" "ERE" "type2" "2"  "10" "user2" "ERE" "type1" "10"  "5"  "user2" "ERE" "type1" "5"  "1"  "user1" "ERE" "type1" "1"  "9"  "user2" "ERE" "type5" "9"  "8"  "user2" "ERE" "type3" "8"  "3"  "user1" "ERE" "type3" "3"  "7"  "user2" "ERE" "type4" "7"  "4"  "user1" "ERE" "type4" "4"  

I tried this but didn't work as expected

SELECT *  FROM userdetails  ORDER BY usertype <> (SELECT usertype FROM userdetails ORDER BY _id DESC LIMIT 1), _id DESC;

是否值得编写此查询,或者最好在 andoird java 文件中执行此逻辑?

最佳答案

要计算每种类型的最高 userID 值,请使用如下查询:

SELECT usertype,
MAX(userID) AS highestID
FROM userdetails
GROUP BY usertype;

然后您可以将其与原始表连接起来,以便该值可用于每一行:

SELECT userdetails.*
FROM userdetails
JOIN (SELECT usertype,
MAX(userID) AS highestIDperType
FROM userdetails
GROUP BY usertype
) USING (usertype)
ORDER BY highestIDperType DESC,
userID DESC;

关于java - 基于第一次排序,再次排序 Sqlite vs Java (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48837312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com