[英]Executing an SQL query to order a list without having to store it as a java object?
我在MySQL中有一個包含3列的表:CategoryID,Score,dateTime。 我需要找到得分最高的前4個類別。 我還必須考慮每個分數的時間戳,因為如果我在較舊的日期在某個類別中得分較高,而在最近的某個日期中該類別得分較低,則最近的分數將主導該類別的分數。 SQL中是否存在查詢以生成結果集? 我在Java中通過將數據存儲為對象來完成此操作,但是如果有一種更有效的方法,我更願意這樣做。
注意:pickRandom是字符串的ArrayList,並且忽略方法checkIfThere()
Scores prevScore = new Scores(0, "AD", new Timestamp(0));
while (rs.next()) {
categoryID = rs.getString("categoryID");
int relScore = rs.getInt("relevanceScore");
Timestamp ts = rs.getTimestamp("date_time");
Scores currScore = new Scores(relScore, categoryID, ts);
// ensures that a Random Barrier Cateogry is prompted
if ((currScore.getScore() >= prevScore.getScore() && (currScore.getDate().compareTo(prevScore.getDate()) > 0) && !checkIfThere(user, categoryID))) {
pickRandom.add(categoryID);
prevScore = currScore;
}
}
表: 分數
'AD', '110', '2015-08-26 14:59:51'
'DC', '110', '2015-08-26 14:59:51'
'DO', '110', '2015-08-26 14:59:51'
'EC', '8.087601288990395', '2015-08-26 14:59:53'
'EC', '110', '2015-08-26 14:59:51'
嘗試以下操作。 inr
派生表依靠唯一的時間/秒時間分段。 也許可以使用id列進行增強。 inr
將在我的樣本數據中獲得所有6個類別。 外部將加入並說只需要4。
create table scores
( id int auto_increment primary key,
categoryID varchar(10) not null,
relevanceScore float not null,
date_time datetime not null
);
truncate table scores;
insert scores (categoryID,relevanceScore,date_time) values
('AD', '110', '2015-08-26 14:59:51'),
('DC', '110', '2015-08-26 14:59:51'),
('DO', '110', '2015-08-26 14:59:51'),
('EC', '8.087601288990395', '2015-08-26 14:59:53'),
('EC', '110', '2015-08-26 14:59:51'),
('OLD1', '110', '2014-08-26 14:59:51'),
('OLD2', '110', '2014-08-26 14:59:51');
select s.* from scores s
join
( select categoryID,max(date_time) as maxdt
from scores
group by categoryId
) inr
on inr.categoryID=s.categoryID and inr.maxdt=s.date_time
order by s.date_time desc
limit 4;
+----+------------+----------------+---------------------+
| id | categoryID | relevanceScore | date_time |
+----+------------+----------------+---------------------+
| 4 | EC | 8.0876 | 2015-08-26 14:59:53 |
| 1 | AD | 110 | 2015-08-26 14:59:51 |
| 2 | DC | 110 | 2015-08-26 14:59:51 |
| 3 | DO | 110 | 2015-08-26 14:59:51 |
+----+------------+----------------+---------------------+
4 rows in set (0.00 sec)
select s.* from scores s
join
( select categoryID,max(date_time) as maxdt
from scores
group by categoryId
) inr
on inr.categoryID=s.categoryID and inr.maxdt=s.date_time
order by s.relevanceScore desc
limit 4;
+----+------------+----------------+---------------------+
| id | categoryID | relevanceScore | date_time |
+----+------------+----------------+---------------------+
| 1 | AD | 110 | 2015-08-26 14:59:51 |
| 2 | DC | 110 | 2015-08-26 14:59:51 |
| 3 | DO | 110 | 2015-08-26 14:59:51 |
| 6 | OLD1 | 110 | 2014-08-26 14:59:51 |
+----+------------+----------------+---------------------+
4 rows in set (0.00 sec)
嘗試這個
select * from Scores order by date_time,relevanceScore;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.