2012-05-10 82 views
1

我正在開發一個簡單的PHP和MySQL項目,它具有簽入和簽出功能。在單個表中合併行顯示重複的結果

我在合併單個表中的行時遇到了一些問題。以下是您可以在不合並行的情況下看到的原始數據。

mysql> SELECT * FROM clock; 
+--------+---------+---------------------+----------+ 
| log_id | user_id | xtimestamp   | text  | 
+--------+---------+---------------------+----------+ 
|  1 | 1000  | 2012-05-10 07:47:06 | CHECKIN | 
|  2 | 1003  | 2012-05-10 07:47:23 | CHECKIN | 
|  3 | 1002  | 2012-05-10 07:47:36 | CHECKIN | 
|  4 | 1002  | 2012-05-10 07:47:49 | CHECKOUT | 
|  5 | 1003  | 2012-05-10 07:48:11 | CHECKOUT | 
|  6 | 1000  | 2012-05-10 07:48:23 | CHECKOUT | 
|  7 | 1000  | 2012-05-10 07:48:52 | CHECKIN | 
+--------+---------+---------------------+----------+ 

這裏是我的SQL查詢來在單個錶行合併有入住和退房視圖。

SELECT a.log_id as 'ID' 
, a.user_id as 'User_ID' 
, a.xtimestamp as 'CHECKIN' 
, b.xtimestamp as 'CHECKOUT' 
FROM clock a, clock b 
WHERE a.info = 'CHECKIN' 
AND b.info = 'CHECKOUT' 
AND a.user_id = b.user_id 
GROUP BY a.log_id; 

這裏是查詢的結果。

+----+---------+---------------------+---------------------+ 
| ID | User_ID | CHECKIN    | CHECKOUT   | 
+----+---------+---------------------+---------------------+ 
| 1 | 1000 | 2012-05-10 07:47:06 | 2012-05-10 07:48:23 | 
| 2 | 1003 | 2012-05-10 07:47:23 | 2012-05-10 07:48:11 | 
| 3 | 1002 | 2012-05-10 07:47:36 | 2012-05-10 07:47:49 | 
| 7 | 1000 | 2012-05-10 07:48:52 | 2012-05-10 07:48:23 | 
+----+---------+---------------------+---------------------+ 

正如你可以在上面的結果可以看出,有4條回報,但它應該是3,因爲在ID 7 USER_ID 1000沒有出記錄的是,ID 1和ID 7具有相同的退房日期。

我想達到的目標是這樣的

+----+---------+---------------------+---------------------+ 
| ID | User_ID | CHECKIN    | CHECKOUT   | 
+----+---------+---------------------+---------------------+ 
| 1 | 1000 | 2012-05-10 07:47:06 | 2012-05-10 07:48:23 | 
| 2 | 1003 | 2012-05-10 07:47:23 | 2012-05-10 07:48:11 | 
| 3 | 1002 | 2012-05-10 07:47:36 | 2012-05-10 07:47:49 | 
| 7 | 1000 | 2012-05-10 07:48:52 | null    | 
+----+---------+---------------------+---------------------+ 

或本

+----+---------+---------------------+---------------------+ 
| ID | User_ID | CHECKIN    | CHECKOUT   | 
+----+---------+---------------------+---------------------+ 
| 1 | 1000 | 2012-05-10 07:47:06 | 2012-05-10 07:48:23 | 
| 2 | 1003 | 2012-05-10 07:47:23 | 2012-05-10 07:48:11 | 
| 3 | 1002 | 2012-05-10 07:47:36 | 2012-05-10 07:47:49 | 
+----+---------+---------------------+---------------------+ 

如果任何人有一個偉大的想法,請分享..謝謝想了很多。

+0

你的問題沒有在其他地方顯示的輸出調和開始在表中顯示的'log_id'值;雖然我不認爲這會影響對您的問題的理解,但您可能希望編輯您的問題以保持一致? – eggyal

+0

@eggyal,你有一個點,你給我的想法和log_id是罪魁禍首。我已經更新了我的SQL,現在它可以工作。 –

+0

您應該將解決方案作爲答案發布。一段時間過後,你將能夠接受它。 – Ilion

回答

1
SELECT check_in.log_id   AS `CHECKIN ID` 
    ,  check_in.user_id   AS `User_ID` 
    ,  check_in.xtimestamp  AS `CHECKIN` 
    ,  MIN(check_out.xtimestamp) AS `CHECKOUT` 
FROM  clock      AS `check_in` 
    JOIN clock      AS `check_out` 
     ON (
      check_out.info = 'CHECKOUT' 
     AND check_in.info = 'CHECKIN' 
     AND check_in.user_id  = check_out.user_id 
     AND check_in.xtimestamp <= check_out.xtimestamp 
    ) 
GROUP BY check_in.log_id, check_in.user_id, check_in.xtimestamp; 
+0

感謝一百萬,這是工作101%。 –

1

也許是這樣的:

SELECT 
    MIN(CASE WHEN info='CHECKIN' THEN xtimestamp ELSE NULL END) AS CHECKIN, 
    MAX(CASE WHEN info='CHECKOUT' THEN xtimestamp ELSE NULL END) AS CHECKOUT, 
    MAX(log_id) AS ID, 
    User_ID 
FROM 
    clock 
GROUP BY 
    User_ID 
+0

您的想法也適用。謝謝。 –

+0

沒問題。很高興提供幫助:P – Arion

相關問題