我有一个存储汽车价格的表格,其结构如下:
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| model | varchar(255) | YES | | NULL | |
| make | varchar(255) | YES | | NULL | |
| year | varchar(255) | YES | | NULL | |
| avg_price | decimal(8,2) | YES | | NULL | |
| median_price | decimal(8,2) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
给定年份的数据可能会在不同时间插入。
For example model 'Honda', make 'Accord' might have results like:
+------+-----------+--------------+--------+---------------------+
| year | avg_price | median_price | model | created_at |
+------+-----------+--------------+--------+---------------------+
| 1992 | 2431.29 | 2000.00 | accord | 2012-02-23 17:31:41 |
| 1993 | 2609.13 | 2195.00 | accord | 2012-02-23 17:31:44 |
| 1994 | 2858.81 | 2400.00 | accord | 2012-02-23 17:31:44 |
| 2000 | 4771.99 | 4450.00 | accord | 2012-02-23 17:31:46 |
| 2001 | 5260.16 | 5000.00 | accord | 2012-02-23 17:31:46 |
| 2000 | 4860.19 | 4795.00 | accord | 2012-08-15 06:09:52 |
| 2001 | 5071.49 | 4990.00 | accord | 2012-08-15 06:09:52 |
| 2002 | 5872.80 | 5795.00 | accord | 2012-08-15 06:09:52 |
| 2003 | 7521.44 | 7950.00 | accord | 2012-08-15 06:09:52 |
| 2004 | 8348.19 | 8495.00 | accord | 2012-08-15 06:09:52 |
我想检索该年汽车制造商的所有最新本田雅阁数据。
所以在上面的例子中,我想从 2012-08-15 06:09:52 检索 2000,2001,2002,2003,2004 年的数据,但较早的年份将从 2012-02-23 17 开始: 31:41
select year,avg_price,median_price,model,created_at
from car_prices
where make='honda' and model= 'accord' group by year asc
上面的查询获取每年的不同数据,但不是每年插入的最后一条记录。
任何想法如何获得每年以及最新的不同数据?