子查询结果出现问题

时间:2015-11-06 11:53:35

标签: mysql

苦苦寻找如何在我的查询结果中显示最大注册金额。目前我只能输出名称。它是一个赋值,它必须是一个子查询。谢谢你的帮助

 SELECT FirstName, LastName
   FROM tblattendees
   WHERE (SELECT MAX(RegistrationPaid) FROM tblregistration
   WHERE tblattendees.AttendeeID = tblregistration.AttendeeID) 
   >= ALL (SELECT  MAX(RegistrationPaid) FROM tblregistration GROUP BY AttendeeID);

1 个答案:

答案 0 :(得分:0)

如果您正确理解了您的查询,则需要一份出席者名单,其中支付的最高注册数等于为任何与会者支付的最高报酬。

如果是这样,可以通过加入几个子查询来完成: -

SELECT FirstName, LastName, sub0.MaxRegistrationPaid
FROM tblattendees
INNER JOIN 
(
    SELECT AttendeeID, MAX(RegistrationPaid) AS MaxRegistrationPaid
    FROM tblregistration
    GROUP BY AttendeeID
) sub0
ON tblattendees.AttendeeID = sub0.AttendeeID
INNER JOIN 
(
    SELECT MAX(RegistrationPaid) AS MaxRegistrationPaid
    FROM tblregistration
) sub1
ON sub0.MaxRegistrationPaid = sub1.MaxRegistrationPaid

也可以使用HAVING完成: -

SELECT FirstName, LastName, MAX(RegistrationPaid) AS MaxRegistrationPaid
FROM tblattendees
INNER JOIN tblregistration
ON tblattendees.AttendeeID = tblregistration.AttendeeID
CROSS JOIN 
(
    SELECT MAX(RegistrationPaid) AS HighestRegistrationPaid
    FROM tblregistration
) sub1
GROUP BY FirstName, LastName
HAVING MaxRegistrationPaid >= HighestRegistrationPaid