假设我有一个表格如下:
loan:
no | credit
-------------
L1 | 600
L2 | 550
L3 | 800
L4 | 800
L5 | 700
如果我想找到最大贷款,我可以简单地这样做:
SELECT MAX(l.no)
FROM loan l
WHERE l.credit = (SELECT MAX(l.credit) from loan l)
但这只会给我一笔贷款。有没有办法在不进行复杂查询的情况下显示L3
和L4
?
答案 0 :(得分:2)
是的,如果你想拥有多行,就不要使用没有group by的聚合函数。
SELECT no, credit
FROM loan l
WHERE l.credit = (SELECT MAX(l.credit) from loan l)
答案 1 :(得分:2)
为什么在选择查询中添加“MAX”?
--delete the MAX here, or else only one record would be fetched.
SELECT l.no
FROM loan l
WHERE l.credit = (SELECT MAX(l.credit) from loan l)
答案 2 :(得分:0)
按信用分组
如下所示
SELECT l.no. 从贷款l 在哪里l.credit =(来自贷款的SELECT MAX(l.credit)) 按信用分组;