2012-08-22 107 views
1

我有存儲車企與像一個結構的最後N條記錄:MySQL的 - 選擇基於列

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

上面的查詢得到插入每一年每一年不同的數據,但不是最後一個記錄。

任何想法如何獲得每年以及最新的獨特數據?

回答

1

對於性能和guarnanteed行爲,你應該創建一個查找並加入上。

在您的情況下,您想要查找任何給定make, model, year組的最新created_at值。下面的代碼中的子查詢是這樣做的。

然後您再次加入您的原始數據,只找到那些具有那些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' 
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 
+0

不,那是行不通的。即使在我更正了您通過切換組輸入的sql後修改了 – truthSeekr

+1

修改。請嘗試。你可以嘗試子查詢。 – sel

+0

整潔。很棒。非常感謝你 – truthSeekr

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' 
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;