2014-03-05 34 views
-1

我需要使用mysql查詢的幫助。MySQL查詢。如何對一個字段進行分組並僅保留其他字段的唯一

所以,我有這樣的

--------------------------------------------------------------- 
ReceivingDateTime | SenderNumber | TextDecoded | UDH   | 
--------------------------------------------------------------- 
2013-01-31 16:12:19 | +70000001111 | Bla-bla-bla | 050003A70201 | 
2013-01-31 16:12:19 | +70000001111 | Bla-bla-bla | 050003A70202 | 
2012-01-20 19:24:21 | +70000001111 | Bla-bla-bla |    | 
2012-01-18 14:14:19 | +70000002222 | Bla-bla-bla |    | 
2012-01-21 13:12:20 | +70000002222 | Bla-bla-bla |    | 
2012-01-15 17:12:10 | +70000003333 | Bla-bla-bla | 050003DC0201 | 
2012-01-15 17:13:18 | +70000003333 | Bla-bla-bla | 050003DC0202 | 

現在我的查詢表是

SELECT 
GROUP_CONCAT(TextDecoded SEPARATOR '') TextDecoded, 
`ID` 
FROM `inbox` 
GROUP BY IF(UDH='',id,SUBSTR(UDH,1,10)) ORDER by `ReceivingDateTime` DESC; 

問題

它的工作原理幾乎是很好,但我希望看到這樣的事情

------------------------------------------------------------- 
ReceivingDateTime | SenderNumber | TextDecoded   | 
------------------------------------------------------------- 
2013-01-31 16:12:19 | +70000001111 | Bla-bla-blaBla-bla-bla | 
2012-01-21 13:12:20 | +70000002222 | Bla-bla-bla   | 
2012-01-15 17:12:10 | +70000003333 | Bla-bla-blaBla-bla-bla | 

我認爲它應該工作:由UDH對TextDecoded進行分組,按日期排序,只保留唯一的SenderNumber,它比其他相同的SenderNumber更新。 (也許這是錯誤的)。對不起,我的法語。

+0

不清楚你在問什麼,你可以添加更簡潔 –

+1

什麼是「id」列? – KrazzyNefarious

+0

我不知道你會不會理解我,但我會盡力解釋。讓我們看看iPhone的消息,你只能看到屏幕上最新的消息,但當你點擊它時,還有更多。在我的情況幾乎相同。即使您將在Mac OS上查看Mail.app,也會收到由發件人分組的所有郵件。你說對了?或需要更多的解釋?:) –

回答

0

編輯: 由於在實踐中會從中找到在內部查詢的第一行返回數據,而MySQL的評論所指出的,它不能保證做到這一點。爲了保證這一點,然後像下面這樣將需要:

SELECT 
    MAX(ReceivingDateTime) AS ReceivingDateTime, 
    SenderNumber, 
    SUBSTRING_INDEX(GROUP_CONCAT(TextDecoded ORDER BY ReceivingDateTime DESC), ',', 1) AS TextDecoded, 
    SUBSTRING_INDEX(GROUP_CONCAT(ID ORDER BY ReceivingDateTime DESC), ',', 1) AS ID 
FROM (
    SELECT 
     MAX(ReceivingDateTime) AS ReceivingDateTime, 
     SUBSTRING_INDEX(GROUP_CONCAT(SenderNumber ORDER BY ReceivingDateTime DESC), ',', 1) AS SenderNumber, 
     GROUP_CONCAT(TextDecoded ORDER BY ReceivingDateTime DESC SEPARATOR '') AS TextDecoded, 
     SUBSTRING_INDEX(GROUP_CONCAT(ID ORDER BY ReceivingDateTime DESC), ',', 1) AS ID 
    FROM inbox 
    GROUP BY IF(UDH='',id,SUBSTR(UDH,1,10)) ORDER BY ReceivingDateTime DESC 
) tbl 
GROUP BY SenderNumber 

原來的答案:

您可以使用子查詢:

SELECT * FROM (
    SELECT 
     `ReceivingDateTime`, 
     `SenderNumber`, 
     GROUP_CONCAT(`TextDecoded` SEPARATOR '') `TextDecoded`, 
     `ID` 
    FROM `inbox` 
    GROUP BY IF(`UDH`='',`ID`,SUBSTR(`UDH`,1,10)) ORDER by `ReceivingDateTime` DESC 
) tbl 
GROUP BY `SenderNumber`; 

您可能還需要確保的內部分組查詢也得到了正確的訂單:

SELECT * FROM (
    SELECT 
     MAX(`ReceivingDateTime`) AS `ReceivingDateTime`, 
     `SenderNumber`, 
     GROUP_CONCAT(`TextDecoded` ORDER BY `ReceivingDateTime` DESC SEPARATOR '') `TextDecoded`, 
     `ID` 
    FROM `inbox` 
    GROUP BY IF(UDH='',`ID`,SUBSTR(`UDH`,1,10)) ORDER by `ReceivingDateTime` DESC 
) `tbl` 
GROUP BY `SenderNumber`; 

這假定UDH的10個字符開始具有獨特的發送方號碼,否則你將需要成才,如:

SUBSTRING_INDEX(GROUP_CONCAT(`SenderNumber` ORDER BY `ReceivingDateTime` DESC), ',', 1) AS `SenderNumber` 

其中列出了相同的順序有關SenderNumbers,然後提取的第一個,以確保我們從包含的行獲取數據MAX(ReceivingDateTime)

+0

外部查詢的GROUP BY的用途是什麼?您沒有使用任何聚合功能。 – AgRizzo

+0

它使SenderNumber獨一無二。當從沒有應用聚合函數的列返回數據時,它只是在它爲GROUP BY子句中的每個唯一值找到的第一行中返回該列的值。因爲內部查詢是有序的,所以這將始終是該SenderNumber的最新條目 – KamiOfTea

+0

爲什麼不使用DISTINCT?在你的情況下,所有其他字段將是隨機的,如果你沒有GROUP BY的所有列沒有聚合函數。 – AgRizzo

相關問題