2015-11-06 91 views
6

我想要發送者和接收者之間的發送者和接收者之間的最近創建日期,其中發送者或接收者= 002,然後由發送者和接收者分組。最後通知002的成員是發送者還是接收者。MYSQL由兩列的最大日期組

這只是一個示例數據。

id sender | receiver |  create_date 
1  001    002   2015-10-13 10:30:01 
2  003    002   2015-11-06 15:30:59 
3  001    002   2015-11-02 05:30:01 
4  001    002   2015-11-03 11:08:22 
5  002    004   2015-10-20 12:15:36 
6  002    004   2015-11-03 17:35:10 
7  002    005   2015-09-01 06:02:20 
8  002    001   2015-11-06 15:10:32 

結果應該是這樣的

id | member | type |  create_date 
2   003   sender  2015-11-06 15:30:59 
6   004   receiver  2015-11-03 17:35:10 
7   005   receiver  2015-09-01 06:02:20 
8   001   receiver  2015-11-06 15:10:32 

到目前爲止,我曾嘗試:

1)

SELECT 
    id, 
    IF(sender <> '002', sender, receiver) AS member, 
    IF(sender <> '002', 'sender', 'receiver') AS type, 
    MAX(create_date) AS max_date 
FROM 
    table 
WHERE 
    sender = '002' OR receiver = '002' 
GROUP BY 
    member 

這是我得到的結果。

id | member | type |  create_date 
1   001   sender  2015-11-06 15:10:32 
2   003   sender  2015-11-06 15:30:59 
5   004   receiver  2015-11-03 17:35:10 
7   005   receiver  2015-09-01 06:02:20 

我努力獲得最新的當前行,但create_date沒有問題。

2)我已經嘗試使用內連接以及。

SELECT 
    * 
FROM 
    table a 
INNER JOIN 
(
    SELECT 
     id, 
     MAX(create_date) as max_date 
    FROM 
     table 
    WHERE 
     sender = '002' OR receiver = '002'   
) b ON b.max_date = a.create_date 

這適用於檢索最新的行記錄。但是如果我需要使用最新的create_date檢索行,但是由發件人和收件人分組呢?

回答

1

可能使用子查詢來獲取成員和類型的日期列表,然後從得到最大: -

SELECT member, type, MAX(create_date) 
FROM 
(
    SELECT sender AS member, 'sender' AS type, create_date 
    FROM table 
    WHERE receiver = '002' 
    UNION 
    SELECT receiver AS member, 'receiver' AS type, create_date 
    FROM table 
    WHERE sender = '002' 
) sub0 
GROUP BY member, type 

該id是一個小問題。如果需要,那麼它需要根據成員/類型/創建日期返回到表格中,或者使用GROUP_CONCAT做一個小提琴: -

SELECT SUBSTRING_INDEX(GROUP_CONCAT(id ORDER BY create_date DESC), ',', 1), 
     member, 
     type, 
     MAX(create_date) 
FROM 
(
    SELECT id, sender AS member, 'sender' AS type, create_date 
    FROM table 
    WHERE receiver = '002' 
    UNION 
    SELECT id, receiver AS member, 'receiver' AS type, create_date 
    FROM table 
    WHERE sender = '002' 
) sub0 
GROUP BY member, type 
1

這是一個可怕的無效的方式做到這一點,但它會獲得正確的結果:

SELECT id, t.member, COALESCE(IF(sender<>2,'sender',NULL),'receiver') type, t.create_date 
FROM a 
JOIN (
SELECT COALESCE(IF(sender<>2,sender,NULL),receiver) member, MAX(create_date) create_date 
FROM a GROUP BY member 
) t on ((a.sender=t.member or a.receiver=t.member) AND a.create_date=t.create_date) 
WHERE a.sender='002' OR a.receiver='002'; 

內子選擇會發現你對每個參與成員和最新CREATE_DATE外選擇會發現,特定的行。

2

你能做到在一個單一的選擇,用的情況下

select 
case when sender='002' then receiver else sender end as member, -- # knowing that either sender or receiver are 002, take the other. 
case when sender='002' then 'receiver' else 'sender' end as type, 
max(date) 
from table 
where sender = '002' or receiver = '002' 
group by 1,2 
+1

這是要走的路,如果您不需要'id'。 – vhu

+1

@vhu真的,我錯過了。如果你想要這個ID,你必須做一個連接回來。如果該ID與創建日期的順序相同,則也可以採用max(id) – AdrianBR