希望这是可能的 -
有这样一张桌子:
Car_ID Brand Model Year
----------------------------------
1 Ford Focus 2012
1 Ford Mustang 1992
2 Toyota Yaris 2005
3 BMW 1s 1995
4 VW Polo 2010
4 VW Golf 2002
4 VW Beetle 2007
并希望它像这样订购:
Car_ID Brand Model Year
----------------------------------
1 Ford Focus 2012
1 Ford Mustang 1992
4 VW Polo 2010
4 VW Beetle 2007
4 VW Golf 2002
2 Toyota Yaris 2005
3 BMW 1s 1995
所以拥有最新型号的品牌是第一个,其次是其他品牌 该品牌的型号按最新顺序排列。
接下来是第二款最新车型和其他车型的品牌 该品牌,最新的
等等。
如果可以做到这一点有什么想法吗?
- 答案 - 我的回答是错误的,当我再测试一些时会回复
答案 0 :(得分:2)
mysql> SELECT cars.*
->
-> FROM cars
-> JOIN ( SELECT Name_ID, MAX(Year) AS max_year
-> FROM cars
-> GROUP BY Name_ID )
-> AS dt_max
-> USING (Name_ID)
->
-> ORDER BY max_year DESC, Name_ID, Year DESC;
+---------+--------+---------+------+
| Name_ID | Brand | Model | Year |
+---------+--------+---------+------+
| 1 | Ford | Focus | 2012 |
| 1 | Ford | Mustang | 1992 |
| 4 | VW | Polo | 2010 |
| 4 | VW | Beetle | 2007 |
| 4 | VW | Golf | 2002 |
| 2 | Toyota | Yaris | 2005 |
| 3 | BMW | 1s | 1995 |
+---------+--------+---------+------+
7 rows in set (0.00 sec)