mysql - 根据列选择最后n条记录

时间:2012-08-22 06:58:48

标签: mysql sql

我有一张表格,用以下结构存储汽车价格:

+-----------------+--------------+------+-----+---------+----------------+
| 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

以上查询获取每年的不同数据,但不会获取每年插入的最后一条记录。

如何获取每年以及最新数据的任何想法?

4 个答案:

答案 0 :(得分:2)

SELECT p.*
FROM car_prices p
JOIN (SELECT year, MAX(created_at) latest
      FROM car_prices
      WHERE make = 'honda' AND model = 'accord'
      GROUP BY year) mp
ON p.year = mp.year AND p.created_at = mp.latest
WHERE p.make = 'honda' AND p.model = 'accord'

答案 1 :(得分:1)

select * from(
select year,avg_price,median_price,model,created_at 
from car_prices 
where make='honda' and    model= 'accord'     
order by created_at desc,year desc) m
group by year asc

答案 2 :(得分:1)

对于性能和保证行为,您应该创建一个查找并加入。

在您的情况下,您希望查找任何给定created_at组的最新make, model, year值。下面代码中的子查询可以做到这一点。

然后再次将其加入原始数据,仅查找具有make, model, year, created_at值的记录。

这意味着您有多个具有相同make, model, year, created_at值的记录,您将获得make, model, year的多个结果。

确保您拥有涵盖(make, model, year, created_at)的索引,以便快速搜索最新的created_at以及加入。

SELECT
  *
FROM
  car_prices
INNER JOIN
(
  SELECT
    make, model, year, MAX(created_at) as created_at
  FROM
    car_prices
  GROUP BY
    make, model, year
)
  AS most_recent
    ON  car_prices.make       = most_recent.make
    AND car_prices.model      = most_recent.model
    AND car_prices.year       = most_recent.year
    AND car_prices.created_at = most_recent.created_at
WHERE
      most_recent.make  = 'honda'
  AND most_recent.model = 'accord'

答案 3 :(得分:0)

添加一个默认设置为0的新布尔列。我使用:is_old

| is_old            | TINYINT(1) | NO  |     | 0    |                |

在为新品牌,型号,年插入价格时,我首先要做:

UPDATE car_prices 
   SET `is_old`=1
WHERE 
   `make`='make_to_insert' AND
   `model`='model_to_insert' AND
   `year`='year_to_insert';

然后我插入新行。

获取近期价格非常简单:

SELECT * FROM car_prices
WHERE `is_old` = 0;

您的示例查询将是:

SELECT * FROM
  car_prices
WHERE
      make  = 'honda'
  AND model = 'accord'
  AND is_old = 0;