Saturday, February 25, 2012

Please help.......Sql problem

I am new in sql .....and now I have 2 tables. The first table name Table1 have 3 feilds
id idName Name
1 223 John
2 32 Tim
3 443 Carty
4 111 Timmy
5 27 Jam
6 790 Max

The second tables name Table2 have 3 fields
idName info Co
32 animal 4
32 product 3
32 city 2
790 hat 3
223 color 2
223 rat 1
111 home 1

I want the result to display the highest number of from Co field ...This is an example of the result that I am trying to do. Anyone have any comment ......... :eek: :o

idName Name info Co
32 Tim animal 4
790 Max hat 3
223 John color 2
111 Timmy home 1this should do:

select *
from tb1 join (select * from tb2 x
where x.co=(select max(co) from tb2 y
where y.idname=x.idname)) t2 on tb1.idname=t2.idname

No comments:

Post a Comment