2014-03-31 76 views
1

表T1獲取最近的日期值組具有以下結構由每個項目

| id |  from    | item | p | 
    1 2014-03-26 08:00:00  500  9 
    2 2014-03-28 14:30:00  500  7 
    3 2014-03-29 14:30:00  200  48 
    4 2014-04-01 19:00:00  200  51 
    5 2014-03-30 23:30:00  500  6 

如何選擇爲每個項目只與日期最近的日期的記錄,現在過去?

SELECT t1.`from`, t1.item, t1.item 
    FROM t1 
WHERE t1.`from` <= NOW() 
    ORDER BY `from` DESC 
LIMIT 1 

這將結果限制爲1項

SELECT t1.`from`, t1.item, t1.item 
    FROM t1 
WHERE t1.`from` <= NOW() 
    GROUP BY item 

現在返回最早的記錄的foreach元素不是nearst來。

我應該使用什麼?

編輯

現在

預期結果(2014-03-31 15:30:00)

| id |  from    | item | p | 
    3 2014-03-29 14:30:00  200  48 
    5 2014-03-30 23:30:00  500  6 

結果我得到:

| id |  from    | item | p | 
    1 2014-03-26 08:00:00  500  9 
    3 2014-03-29 14:30:00  200  48 

服務器時間是完美的!

EDIT2

SELECT t1.`from`, t1.p, prod_desc.name AS prod 
    FROM PROD_DESC 
    JOIN 
    (SELECT MAX(t1.`from`) `from`, t1.id 
     FROM t1 
    GROUP BY prod) t2 USING(`from`, id) 
    LEFT JOIN t1 
    ON t1.item = PROD_DESC.id_prod 
WHERE t1.`from` <= NOW() 
+0

應該期望的結果集是什麼樣的......,是有一個sqlfiddle? – Strawberry

+1

我編輯了我的答案與預期的結果。 'sqlfiddle.com'已關閉,我收到一個運行時錯誤 – Perocat

+0

它發生了。幾分鐘後再試。或者在這裏粘貼DDL。 – Strawberry

回答

2

DDLs ...

DROP TABLE IF EXISTS my_table; 

CREATE TABLE my_table 
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY 
,`from`    DATETIME NOT NULL 
,item INT NOT NULL 
,p INT NOT NULL 
); 

INSERT INTO my_table VALUES 
(1,'2014-03-26 08:00:00',500,9), 
(2,'2014-03-28 14:30:00',500,7), 
(3,'2014-03-29 14:30:00',200,48), 
(4,'2014-04-01 19:00:00',200,51), 
(5,'2014-03-30 23:30:00',500,6); 

SELECT * FROM my_table; 
+----+---------------------+------+----+ 
| id | from    | item | p | 
+----+---------------------+------+----+ 
| 1 | 2014-03-26 08:00:00 | 500 | 9 | 
| 2 | 2014-03-28 14:30:00 | 500 | 7 | 
| 3 | 2014-03-29 14:30:00 | 200 | 48 | 
| 4 | 2014-04-01 19:00:00 | 200 | 51 | 
| 5 | 2014-03-30 23:30:00 | 500 | 6 | 
+----+---------------------+------+----+ 

解決方案:

SELECT x.* 
    FROM my_table x 
    JOIN 
    (SELECT item 
      , MAX(`from`) max_from 
     FROM my_table 
     WHERE `from` <= NOW() 
     GROUP 
      BY item 
    ) y 
    ON y.item = x.item 
    AND y.max_from = x.`from`; 
+----+---------------------+------+----+ 
| id | from    | item | p | 
+----+---------------------+------+----+ 
| 3 | 2014-03-29 14:30:00 | 200 | 48 | 
| 5 | 2014-03-30 23:30:00 | 500 | 6 | 
+----+---------------------+------+----+ 
+0

WOOOOOOOOW!那很完美!!!謝謝!!!還有一個問題:爲什麼'FROM my_table x'而不是'FROM my_table AS x'?一樣?還有'BY item(AS)y'? – Perocat

+0

少打字 - 不要使用保留字作爲列/表標識符(yuk) – Strawberry

+0

是的,對不起!我簡單地將它從意大利語'da'翻譯成英文'from'。再次感謝你! – Perocat

-2

轉換的日期戳,然後在最終極限的結果爲1,這樣你只有最近的一個距離當前時間的時間戳差排序。

0

你試過:

......而最後一個問題後更改這又是真實的:條件後

SELECT t1.`from`, t1.item, t1.item 
FROM t1 
WHERE t1.`from` <= NOW() 
GROUP BY item 
ORDER BY t1.`from` DESC 

編輯改變

SELECT t1.`from`, t1.item, t1.item 
FROM t1 
GROUP BY item 
ORDER BY ABS(TIMESTAMPDIFF(MICROSECOND,t1.`from`,NOW())) 

而再次編輯以進行最終編輯

這次我有了數據庫並檢查了結果 - 它們與你所期望的相符!

SELECT t2.`from`, t2.item, t2.item 
FROM (SELECT * FROM t1 WHERE t1.`from` <= NOW() ORDER BY t1.`from` DESC) AS t2 
GROUP BY t2.item 
+0

不返回最近的日期,但最舊的 – Perocat

+0

@Perocat - 現在檢查它 – blue

+0

和以前一樣! – Perocat

1

這可以通過使用自做加盟的最大值

SELECT t.* 
FROM t1 t 
JOIN 
(SELECT MAX(t1.`from`) `from`,item ,t1.id 
FROM t1 t1 
WHERE `from` <= NOW() 
GROUP BY item 
) t2 USING(`from`,item) 

@Strawberry他是正確的關於where子句的位置,也是加入應該是USING(,item)複合條件

+0

我認爲你是WHERE子句是(技術上)在錯誤的地方 – Strawberry

+0

我發佈了這個完整的查詢集成,但我得到了一些錯誤...(查看問題) – Perocat

+0

@Perocat你已經發布? *我得到了一些錯誤*你可以提及他們 –