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 … 4Task: 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

解决方案 »

  1.   

    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[align=center]====  ====
    [/align]
      

  2.   

    做 CVS ?[align=center]====  ====
    [/align]