2011-08-11 70 views
8

我有一個表中的以下數據:的MySQL - 顯示記錄對一個記錄

Time, Type, Kilometers 
12:00, 1, 0.1 
12:30, 2, 0.2 
14:00, 1, 0.4 
15:00, 2, 1.0 
16:00, 1, 1.2 
16:30, 2, 1.5 
16:45, 1, 2.0 

該數據按時間順序使用日期時間字段排序。我想顯示這些記錄「對」爲1行,像這樣:

StartTime, Type1Km, Type2Km 
12:00, 0.1, 0.2 
14:00, 0.4, 1.0 
16:00, 1.2, 1.5 
16:45, 2.0, NULL 

有幾個需要注意的地方:如果沒有類型1開始,然後顯示在結果表Type1Km場NULL。同樣,如果沒有Type2結束,則在記錄的Type2Km字段中顯示NULL。

我怎麼能這樣做?

+0

棘手的部分是,你加入類型1的時間與下一次類型2.如果有2個類型1的「連續」,那麼Type2Km應該爲null,對吧? – MPelletier

+0

@MPelletier - 正確(這是不太可能發生,但如果解決方案考慮到這將是有益的) – Simon

+0

我會更容易解決,如果這個表有一個自動遞增列,爲連接至X + 1差別測試...有沒有一個,只是沒有在上面介紹? – DRapp

回答

4

不幸的是,MySQL的缺乏FULL OUTER JOIN,所以你必須要UNION兩套在一起。

這將讓你在哪兒Type1Km存在,Type2Km是否確實的情況。

SELECT 
    t1.`Time` as StartTime, 
    t1.`Kilometers` as Type1Km, 
    t2.`Kilometers` as Type2Km 
FROM `times` t1 
LEFT JOIN `times` t2 ON t2.`Type` = 2 
        AND t2.`Time` = (SELECT `Time` FROM `times` 
            WHERE `Time` > t1.`Time` 
            ORDER BY `Time` LIMIT 1) 
WHERE t1.`Type` = 1 

現在我們需要Type1Km不存在的情況。

SELECT 
    t2.`Time` as StartTime, 
    NULL as Type1Km, 
    t2.`Kilometers` as Type2Km 
FROM `times` t2 
LEFT JOIN `times` t1 ON t1.`Time` = (SELECT `Time` FROM `times` 
            WHERE `Time` < t2.`Time` 
            ORDER BY `Time` DESC LIMIT 1) 
WHERE t2.`Type` = 2 
    AND (t1.`Type` = 2 OR t1.`Type` IS NULL) 

UNION那些在一起,你所期望的結果:

(
SELECT 
    t1.`Time` as StartTime, 
    t1.`Kilometers` as Type1Km, 
    t2.`Kilometers` as Type2Km 
FROM `times` t1 
LEFT JOIN `times` t2 ON t2.`Type` = 2 
        AND t2.`Time` = (SELECT `Time` FROM `times` 
            WHERE `Time` > t1.`Time` 
            ORDER BY `Time` LIMIT 1) 
WHERE t1.`Type` = 1 

) UNION ALL (

SELECT 
    t2.`Time` as StartTime, 
    NULL as Type1Km, 
    t2.`Kilometers` as Type2Km 
FROM `times` t2 
LEFT JOIN `times` t1 ON t1.`Time` = (SELECT `Time` FROM `times` 
            WHERE `Time` < t2.`Time` 
            ORDER BY `Time` DESC LIMIT 1) 
WHERE t2.`Type` = 2 
    AND (t1.`Type` = 2 OR t1.`Type` IS NULL) 
) 

ORDER BY `StartTime` 

更新

在我以前的查詢,我忘了考慮有一個「2型「記錄在一開始。更新爲解釋這一點。這裏的結果我得到:

數據在times表:

+----------+------+------------+ 
| Time  | Type | Kilometers | 
+----------+------+------------+ 
| 11:00:00 | 2 |  0.1 | 
| 12:00:00 | 1 |  0.1 | 
| 12:30:00 | 2 |  0.2 | 
| 14:00:00 | 1 |  0.4 | 
| 14:30:00 | 1 |  0.8 | 
| 15:00:00 | 2 |  1.0 | 
| 15:30:00 | 2 |  0.2 | 
| 16:00:00 | 1 |  1.2 | 
| 16:30:00 | 2 |  1.5 | 
| 16:45:00 | 1 |  2.0 | 
+----------+------+------------+ 

查詢的結果:

+-----------+---------+---------+ 
| StartTime | Type1Km | Type2Km | 
+-----------+---------+---------+ 
| 11:00:00 | NULL |  0.1 | 
| 12:00:00 |  0.1 |  0.2 | 
| 14:00:00 |  0.4 | NULL | 
| 14:30:00 |  0.8 |  1.0 | 
| 15:30:00 | NULL |  0.2 | 
| 16:00:00 |  1.2 |  1.5 | 
| 16:45:00 |  2.0 | NULL | 
+-----------+---------+---------+ 
+0

UNION的+1。我也在想:) – MPelletier

0

如果可以的話,改變你的表結構有一個type1km和type2km,然後或者在添加公里時更新,或者在選擇時總結。

0

這可能會實現

select 
max(Time), 
substring_index(group_concat(if(type=2,null,Kilometers) order by type),',',1), 
substring_index(group_concat(if(type=1,null,Kilometers) order by type desc),',',1) 
from your_table 
group by date_format(Time, '%H'); 

我按小時假設組,也沒什麼看頭你的榜樣結果是不準確的

爲了更好地解釋這一有趣查詢做什麼

group_concat(if(type=2,null,Kilometers) order by type 
-- this is ensure the Kilometers must NOT from type=2 

group_concat(if(type=1,null,Kilometers) order by type desc 
-- this is ensure the Kilometers must NOT from type=1