读书人

另一道sql有关问题高人请进

发布时间: 2012-03-18 13:55:39 作者: rapoo

另一道sql问题,高人请进!
4.Sample tables (company_id RK companies.id):

Companies
idcompany
1…
2…
3…

Locations
idcompany_idlocationrevision
11…1
22…1
31…2
41…3
52…2
61…4

Task: write an SQL query to select values from the following columns (companies.company, locations.location) where locations.revision value will be the highest for each company


[解决办法]


SQL code
select c.company,l.locationfrom ( Companies c inner join (select company_id,max(revision) as maxrevisionfrom Locations) m on c.id = m.company_id) inner join Locations l on m.maxrevision=l.revision and m.company_id=l.company_id 

读书人网 >Mysql

热点推荐