英文:
Simple SQL query on highest ID
问题
I unsuccessfully attempted to leverage Java's DerivedQueries but cannot accomplish the required result so I have to manually write a SELECT Statement.
我曾尝试利用Java的DerivedQueries,但无法完成所需的结果,因此必须手动编写SELECT语句。
I want to display one single record in my UI. This should be the most recently generated record (which means it has the highest ID Number) associated with a category that we call "ASMS". In other words, look through all the rows that have ASMS#123, find the one that has the highest ID and then return the contents of one column cell.
我想在我的用户界面中显示一条记录。这应该是最近生成的记录(也就是具有最高ID号码的记录),与我们称之为“ASMS”的类别相关联。换句话说,浏览所有具有ASMS#123的行,找到具有最高ID的行,然后返回一个列单元格的内容。
ASMS: Entries are classified by 11 specific ASMS numbers.
ID: AutoGenerated
PPRECORD: New entries being inserted each day
ASMS:条目根据11个特定的ASMS编号进行分类。
ID:自动生成
PPRECORD:每天插入新条目
I hope the image makes more sense.
我希望这张图片更容易理解。
The ReactJS FE makes an AXIOS.get and I can retrieve all the records associated with the ASMS, but I do not have the skill to display only JSON object that has the highest ID value. I'm happy to do this in the FE also.
ReactJS前端使用AXIOS.get,我可以检索与ASMS相关的所有记录,但我不具备仅显示具有最高ID值的JSON对象的技能。我也可以在前端完成这个操作。
I tried Derived Queries. .findFirst1ByAsmsNumber(asmsNumber)
does not consider the highest ID number.
我尝试了Derived Queries。.findFirst1ByAsmsNumber(asmsNumber)
不考虑最高的ID号码。
英文:
I unsuccessfully attempted to leverage Java's DerivedQueries but cannot accomplish the required result so I have to manually write a SELECT Statement.
I want to display one single record in my UI. This should be the most recently generated record (which means it has the highest ID Number) associated with a category that we call "ASMS". In other words, look through all the rows that have ASMS#123, find the one that has the highest ID and then return the contents of one column cell.
ASMS: Entries are classified by 11 specific ASMS numbers.
ID: AutoGenerated
PPRECORD: New entries being inserted each day
I hope the image makes more sense.
//RETURN ONLY THE LATEST RECORD
//https://besterdev-api.apps.pcfepg3mi.gm.com/api/v1/pprecords/latest/{asmsnumber}
@RequestMapping("/pprecords/latest/{asmsNumber}")
public List<Optional<PriorityProgressEntity>> getLatestRecord(@PathVariable(value = "asmsNumber") String asmsNumber) {
List<Optional<PriorityProgressEntity>> asms_number = priorityprogressrepo.findFirst1ByAsmsNumber(asmsNumber);
return asms_number;}
The ReactJS FE makes an AXIOS.get and I can retrieve all the records associated with the ASMS, but I do not have the skill to display only JSON object that has the highest ID value. I'm happy to do this in the FE also.
I tried Derived Queries. .findFirst1ByAsmsNumber(asmsNumber)
does not consider the highest ID number.
答案1
得分: 0
尝试这个:
SELECT pprecord FROM YourTable WHERE id =
(SELECT MAX(id) FROM YourTable WHERE asms = '188660')
解释:
第一行选择 pprecord,第二行选择 id。
如果有任何其他问题,我会改进答案。感谢点赞和采纳~
英文:
Try this:
SELECT pprecord FROM YourTable WHERE id =
(SELECT MAX(id) FROM YourTable WHERE asms = '188660')
Explanation:
First line select pprecord, second line select the id
I'll improve the answer if any additional question. Upvotes and acceptions are appreciated~
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论