select discount.RATE, customer.NAME
from APP.DISCOUNT_CODE as discount
LEFT JOIN APP.CUSTOMER as customer
ON discount.DISCOUNT_CODE = customer.DISCOUNT_CODE;
当我使用这个查询时,我得到了所有的折扣率和客户名称,但我只想显示一个有最大折扣的客户。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;
但是我得到一个错误..如何解决这个问题..
这是第一个查询的表..
错误是 get 正在使用 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.
最佳答案
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
关于MySQL - JOIN - 在另一个表中找到最大值并显示第一个表中的客户名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43003214/