我有一个返回两列最大差异的查询。让我们说它是这样的:
| result |
5
5
5
4
4
3
2
我怎样才能让它每次都返回所有顶级结果? (5,5,5)
我不是在寻找LIMIT 3,因为结果有所不同,有时只有一个顶级数字等。
我现在的查询如下:
SELECT MAX(column1 - column2) as result
FROM table
WHERE othercolumn = somecondition
GROUP by id
ORDER BY results asc
答案 0 :(得分:0)
未经测试但可能有效
select *,column1-column2 as result from table where column1 - column2 =
(SELECT (column1 - column2) as G
FROM table
ORDER BY G DESC limit 1)
答案 1 :(得分:0)
SELECT MAX(column1 - column2) as result
FROM table
WHERE othercolumn = somecondition
GROUP by id
HAVING result =
(select max(column1 - column2) FROM table WHERE othercolumn = somecondition)
ORDER BY id;