简单的SQL查询,获取最高的ID。

huangapple go评论68阅读模式
英文:

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对象的技能。我也可以在前端完成这个操作。

简单的SQL查询,获取最高的ID。

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.

简单的SQL查询,获取最高的ID。

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~

huangapple
  • 本文由 发表于 2023年2月18日 04:36:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75489049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定