MySQL查询。如何对一个字段进行分组并仅保留其他字段的唯一字段

时间:2014-03-05 21:36:32

标签: php mysql sql

我需要有关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。 (也许这是错的)。对不起我的法语。

1 个答案:

答案 0 :(得分: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)的行中获取数据