如何将此查询转换为子查询sql

d7v8vwbk  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(474)

我怎样才能改变这个查询,我想把最大值变成一个子查询,当我输入sql时,唯一会出现的是id和名字。

SELECT staff.staff_id,staff.firstname,max(payment.amount)
from payment ,staff
where payment.staff_id=staff.staff_id
kb5ga3dv

kb5ga3dv1#

JOIN 到(相关)子查询:

SELECT staff.staff_id, staff.firstname,
       (SELECT MAX(payment.amount) 
        FROM payment
        WHERE payment.staff_id = staff.staff_id)
FROM staff
vltsax25

vltsax252#

使用显式联接

SELECT staff.staff_id,staff.firstname,max(payment.amount)
from payment join staff
on  payment.staff_id=staff.staff_id

但我想你想

SELECT staff.staff_id,staff.firstname,p.amount
    from payment p join staff
    on  p.staff_id=staff.staff_id
    where p.amount= select max(amount) from payment
ojsjcaue

ojsjcaue3#

可以尝试使用相关子查询

SELECT staff.staff_id,staff.firstname
from payment inner join staff
on payment.staff_id=staff.staff_id where payment.amount in (select max(payment.amount) from payment)

相关问题