内部连接以获取列值最大的行

cedebl8k  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(349)

我一直在尝试获取列值为max的行的值,但不知怎么的,它不起作用。请帮帮忙。
让我解释一下这个问题
表1 name=agency列agency\u id,agency\u name
表2 name=client列client\u name,total\u bill,agency\u id
我想要得到的是客户名称、代理名称和total\u bill value为最大值的行的total\u bill。
这是我使用的查询

SELECT a.agency_name 
FROM agency a 
INNER JOIN (SELECT agency_id, client_name, MAX(total_bill) total_bill FROM client GROUP BY agency_id ) c 
ON a.agency_id = c.agency_id;

你能告诉我这件事哪里不对吗?
我得到这个错误-

Expression #2 of SELECT list is not in GROUP BY clause and contains 
nonaggregated column 'agency-app.client.client_name' which is not 
functionally dependent on columns in GROUP BY clause; this is incompatible
with sql_mode=only_full_group_by

一定要告诉我怎么解决这个问题。

nxagd54h

nxagd54h1#

尝试此查询:

SELECT client.name, agency.name, client.total_bill
FROM agency, client 
WHERE agency.agency_id=client.agency_id 
AND client.total_bill=(SELECT MAX(total_bill) FROM client)
wgxvkvu9

wgxvkvu92#

如果您想成为总的顶级客户,您可以加入、订购和限制:

select 
    a.agency_name,
    c.client_name,
    c.total_bill
from agencies a
inner join clients c on c.agency_id = a.agency_id
order by c.total_bill desc
limit 1

相关问题