1

我有一个存储汽车价格的表格,其结构如下:

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

4 回答 4

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'
于 2012-08-22T07:19:22.790 回答
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
于 2012-08-22T07:01:29.863 回答
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'
于 2012-08-22T07:22:18.203 回答
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;
于 2017-02-12T10:16:57.107 回答