[英]selecting multiple max values
i have a table like this on a mysql database: 我在mysql数据库上有这样的表:
id | item
-----------
1 | 2
2 | 2
3 | 4
4 | 5
5 | 8
6 | 8
7 | 8
i want the result to be 3 record with the highest Item value 我希望结果是具有最高Item值的3条记录
select max(item) returns only 1 value how can i select multiple max values? select max(item)仅返回1个值如何选择多个最大值? thank you
谢谢
You can use a derived table to get the maximum value and join
it back to the original table to see all rows corresponding to it. 您可以使用派生表获取最大值,然后
join
其联接回原始表以查看与其对应的所有行。
select t.id, t.item
from tablename t
join (select max(item) as mxitem from tablename) x
on x.mxitem = t.item
Edit: 编辑:
select t.co_travelers_id, t.booking_id, t.accounts_id
from a_co_travelers t
join (select accounts_id, max(booking_id) as mxitem
from a_co_travelers
group by accounts_id) x
on x.mxitem = t.booking_id and t.accounts_id = x.accounts_id
If you use an 'aggregate function' without GROUP BY
only one row will be returned. 如果您使用不带
GROUP BY
的“聚合函数”,则仅返回一行。
You may use GROUP BY , with aggregate functions. 您可以将GROUP BY与聚合函数一起使用。
Here is SQLFiddle Demo 这是SQLFiddle演示
SELECT id,max(item) AS item
FROM table_name
GROUP BY id
ORDER BY item DESC
LIMIT 3
Hope this helps. 希望这可以帮助。
There is the graphical explanation . 有图形说明 。 There is script mysql (low abstraction level, no inner join or sth)
有脚本mysql(低抽象级别,没有内部联接或某物)
select * from ocena, uczen where ocena.ocena = (SELECT MAX(ocena.ocena) FROM ocena WHERE ocena.przedmiot_id="4" and ocena.uczen_id="1") and ocena.uczen_id=uczen.id and ocena.przedmiot_id="4" and uczen_id="1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.