sql - how to get row having least value from a table in mysql -


i have written mysql query comprising couple of joins, , result follows.

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 have result group (group by) title having 'least rent'.

plz help.


i have following result set

    id | title | type    |    rent     ------------------------------     1  |  abc  |   low  |     100     ------------------------------     2  |  xyz  |   mid  |     150     ------------------------------     3  |  mno  |   mid  |     200     ------------------------------ 

you can this:

select t1.* tablename t1 inner join (    select title, min(rent) minrent    tablename    group title  ) t2  on t1.title = t2.title          , t1.rent  = t2.minrent; 

sql fiddle demo

this give you:

| id | title | type | rent | ---------------------------- |  1 |   abc |  low |  100 | |  2 |   xyz |  mid |  150 | |  3 |   mno |  mid |  200 | 

this give least rent each title.


however: if looking tile has latest rent can this:

select t1.* tablename t1 order rent   limit 1 

updated sql fiddle demo

this give row:

| id | title | type | rent | ---------------------------- |  1 |   abc |  low |  100 | 

note that: if there duplicates rent has same min rent, won't give duplicate. in case use way @user2009463 suggested.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -