SQL查询返回结果,省略最大值

时间:2018-08-13 20:43:39

标签: php mysql sql phpmyadmin

我有一个表“ Files”,其中存储了文件及其版本,除了最新版本的文件外,我还需要从表中提取所有文件名。

    Name_File             Version
     file_1               1.2
     file_1               1.3
     file_1               1,4
     file_1               2.0
     file_2               1.1
     file_2               1.3      
     file_2               1.6
     file_2               2.0
     file_3               2.4
     file_3               2.5
     file_3               3.0
     etc...

预期结果:

    Name_File             Version
     file_1               1.2
     file_1               1.3
     file_1               1,4        
     file_2               1.1
     file_2               1.3      
     file_2               1.6         
     file_3               2.4
     file_3               2.5

请帮助

1 个答案:

答案 0 :(得分:1)

像这样?

SELECT t1.*
FROM Files t1 LEFT JOIN (SELECT t3.Name_File, MAX(t3.Version) AS Version FROM Files t3 GROUP BY t3.Name_File) t2
 ON (t1.Name_File = t2.Name_File AND t1.Version < t2.Version)
WHERE t2.Version IS NOT NULL

工作Fiddle