MySQL - JOIN - 在另一个 table 中找到最大值并从第一个 table 开始显示客户名称

MySQL - JOIN - find the max value in another table and display customer name from first table

select discount.RATE, customer.NAME 
from APP.DISCOUNT_CODE as discount 
LEFT JOIN APP.CUSTOMER as customer 
ON discount.DISCOUNT_CODE = customer.DISCOUNT_CODE;

当我使用这个查询时,我得到了所有的折扣率和客户名称,但我只想显示一个最大 discount.RATE..

的客户

我试过这个查询..

select max(discount.RATE), customer.NAME 
from APP.DISCOUNT_CODE as discount 
LEFT JOIN APP.CUSTOMER as customer 
ON discount.DISCOUNT_CODE = customer.DISCOUNT_CODE;

但是我得到一个错误..如何解决这个问题..

这是第一个查询的 table..

错误是 运行 使用 max(discount.RATE) ,

的第二个查询
[Exception, Error code 30,000, SQLState 42Y35] Column reference 'CUSTOMER.NAME' is invalid. When the SELECT list contains at least one aggregate then all entries must be valid aggregate expressions.  

首先:您必须在第二个查询中写 GROUP BY

SELECT max(discount.RATE) AS MAX_DISC_RATE
    ,customer.NAME
FROM APP.DISCOUNT_CODE AS discount
LEFT JOIN APP.CUSTOMER AS customer ON discount.DISCOUNT_CODE = customer.DISCOUNT_CODE
GROUP BY customer.NAME;
SELECT
    discount.RATE, customer.NAME
    FROM
        APP.DISCOUNT_CODE as discount
        JOIN APP.CUSTOMER as customer ON discount.DISCOUNT_CODE = customer.DISCOUNT_CODE
    ORDER BY
        discount.RATE DESC
    LIMIT 1