[英]how to get ID of field based on the Max of another field in same table
我有一个名为ae_types
的表。 它包含与我的问题有关的三个字段:
aetId This is Auto Increment and is the Primary Key
aetProposalType Text field 5 characters long
aetDaysToWait Byte data type
aetProposalType
和aetDaysToWait
是在一个独特的密钥,这样我保证永远不会有两个aetProposalType
s的相同aetDaysToWait
。
我正在寻找的结果是获得aetId
领域最大的一起aetDaysToWait
每个aetProposalType
。
以下是我提出的用于完成此操作的查询,但在我看来,它可能不必要地复杂且不是很漂亮。
SELECT ae_types.aetId AS lastEmailId, ae_types.aetProposalType
FROM ae_types INNER JOIN
(SELECT ae_types.aetProposalType, Max(ae_types.aetDaysToWait) AS MaxOfaetDaysToWait
FROM ae_types GROUP BY ae_types.aetProposalType) AS ae_maxDaysToWaitByProposalType
ON (ae_types.aetDaysToWait = ae_maxDaysToWaitByProposalType.MaxOfaetDaysToWait)
AND (ae_types.aetProposalType = ae_maxDaysToWaitByProposalType.aetProposalType);
有哪些替代解决方案,为什么它们会更好?
附注:如果您有任何疑问,请询问,我们很乐意为您提供答案。
我也是那样做的。
select a.aetId, a.aetProposalType, a.aetDaysToWait
from ae_types a
inner join (select aetProposalType, max(aetDaysToWait) as MaxDays
from ae_types
group by aetProposalType) sq
on a.aetProposalType = sq.aetProposalType
and a.aetDaysToWait = sq.MaxDays
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.