简体   繁体   English

MYSQL将联接转换为子查询

[英]MYSQL Convert join to subquery

I'm try to convert a join query to not have the join and use subqueries only but for the life of me I can not figure out a way. 我试图将联接查询转换为不具有联接,而仅使用子查询,但是对我而言,我不知道一种方法。

TABLES: 表格:

表格1 表2

With these tables I'm trying to do the following: Using subqueries get the FirstName, LastName and registration paid of the attendees who have paid the most registration. 使用这些表,我尝试执行以下操作:使用子查询获取注册费用最高的与会者的名字,姓氏和注册费用。

This query produces that result using a join. 该查询使用联接产生该结果。

SELECT FirstName, LastName, SUM(tblregistration.RegistrationPaid) AS `AmountPaid`
FROM tblregistration, tblattendees
WHERE tblregistration.AttendeeID = tblattendees.AttendeeID
GROUP BY tblregistration.AttendeeID 
ORDER BY `AmountPaid` DESC 
LIMIT 5

结果

Is there a way to do this? 有没有办法做到这一点? Thanks. 谢谢。

This should work : 这应该工作:

SELECT
  (SELECT FirstName FROM tblattendees WHERE tblregistration.AttendeeID = tblattendees.AttendeeID) AS `FirstName`,
  (SELECT LastName FROM tblattendees WHERE tblregistration.AttendeeID = tblattendees.AttendeeID) AS `LastName`,
  SUM(tblregistration.RegistrationPaid) AS `AmountPaid`
FROM tblregistration
GROUP BY tblregistration.AttendeeID
ORDER BY `AmountPaid` DESC
LIMIT 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM