[英]how to get row having least value from a table in mysql
I have a written a mysql query comprising a couple of joins, and the result is as follows. 我写了一个包含几个联接的mysql查询,结果如下。
id | title | type | rent
------------------------------
1 abc low 100
2 xyz low 200
2 xyz mid 150
2 xyz high 300
3 mno low 600
3 mno mid 200
I would like to have result that group ( group by
) title
having 'least rent'. 我想让那个组(
group by
) title
具有“最低租金”的结果。
PLZ help. PLZ帮助。
I would like to have the following result set 我想要以下结果集
id | title | type | rent
------------------------------
1 | abc | low | 100
------------------------------
2 | xyz | mid | 150
------------------------------
3 | mno | mid | 200
------------------------------
You can do this: 你可以这样做:
SELECT t1.*
FROM tablename AS t1
INNER JOIN
(
SELECT title, MIN(rent) AS minrent
FROM tablename
GROUP BY title
) AS t2 ON t1.title = t2.title
AND t1.rent = t2.minrent;
This will give you: 这将为您提供:
| ID | TITLE | TYPE | RENT |
----------------------------
| 1 | abc | low | 100 |
| 2 | xyz | mid | 150 |
| 3 | mno | mid | 200 |
This will give you the least rent for each title
. 这样每个
title
租金最少。
However: If you are looking for only the tile which has the latest rent
then you can do this: 但是:如果您只寻找
rent
最近的瓷砖,则可以执行以下操作:
SELECT t1.*
FROM tablename AS t1
ORDER BY rent
LIMIT 1
This will give you this row: 这将为您提供这一行:
| ID | TITLE | TYPE | RENT |
----------------------------
| 1 | abc | low | 100 |
Note that: If there are duplicates Rent
that has the same min rent, then this won't give you those duplicate. 请注意:如果有重复的
Rent
与最低租金相同,则不会为您提供这些重复。 In this case use the way @user2009463 suggested. 在这种情况下,请使用@ user2009463建议的方式。
select * from table_name
where rent = (select min(rent) from table_name)
group by title;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.