日期:2014-05-16  浏览次数:20685 次

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

Companies
id company
1 …
2 …
3 …

Locations
id company_id location revision
1 1 … 1
2 2 … 1
3 1 … 2
4 1 … 3
5 2 … 2
6 1 … 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.location
from ( Companies c inner join (select company_id,max(revision) as maxrevision
from Locations) m on c.id = m.company_id) inner join Locations l on m.maxrevision=l.revision and m.company_id=l.company_id