2014-03-05 33 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

相关问题