MySQL为每个唯一列对返回单个结果

时间:2014-12-22 17:38:53

标签: php mysql sql

这是用户#129的表格。消息是TO或FROM他。

我想要用户发送/接收的最新消息(例如128,130,131 ......等)


id msgTo msgFrom msgContent msgDate

1   129 130 Birmingham  2014-12-19 8:58
2   130 129 Montgomery  2014-12-19 10:59
3   129 131 Anchorage   2014-12-19 11:21
4   131 129 Phoenix 2014-12-19 11:24
5   131 129 Tucson  2014-12-19 11:32
6   128 129 Mesa    2014-12-19 11:34
7   129 128 Chandler    2014-12-19 11:36
8   129 131 Glendale    2014-12-19 11:47
9   131 129 Gilbert 2014-12-19 11:49
10  129 130 Scottsdale  2014-12-19 16:51

期望的结果--->

id  msgTo msgFrom msgContent msgDate

10  129 130 Scottsdale  2014-12-19 16:51
9   131 129 Gilbert 2014-12-19 11:49
7   129 128 Chandler    2014-12-19 11:36

5 个答案:

答案 0 :(得分:1)

SELECT * FROM TABLE_NAME
WHERE msgTo = 129 OR msgFrom = 129
ORDER BY msgDate DESC
LIMIT 3;

答案 1 :(得分:1)

如果整个表格特定于用户#129,那么重组表格可能是您最好的选择。像这样:

TABLE USER_129_MESSAGES

id otherUser msgType msgContent  msgDate

1   130      from    Birmingham  2014-12-19 8:58
2   130      to      Montgomery  2014-12-19 10:59
3   131      from    Anchorage   2014-12-19 11:21
4   131      to      Phoenix     2014-12-19 11:24
5   131      to      Tucson      2014-12-19 11:32
6   128      to      Mesa        2014-12-19 11:34
7   128      from    Chandler    2014-12-19 11:36
8   131      from    Glendale    2014-12-19 11:47
9   131      to      Gilbert     2014-12-19 11:49
10  130      from    Scottsdale  2014-12-19 16:51

然后你可以简单地运行:

SELECT * FROM USER_129_MESSAGES
GROUP BY otherUser
ORDER BY msgDate DESC
LIMIT 3;

答案 2 :(得分:0)

您可以通过msgDate字段订购结果,并在主查询中设置结果限制:

select * from [your_table]
where msgTo=[user id] or msgFrom=[user id]
order by msgDate desc
limit 3

答案 3 :(得分:0)

SELECT * FROM TABLE_NAME WHERE msgTo = 129 OR msgFrom = 129

如果你还想订购这行,你可以这样说:

SELECT * FROM TABLE_NAME WHERE msgTo = 129 OR msgFrom = 129 ORDER BY id DESC

SELECT * FROM TABLE_NAME WHERE msgTo = 129 OR msgFrom = 129 ORDER BY id ASC

答案 4 :(得分:0)

我不知道你是否找到了答案。在搜索同样的问题时,我遇到了这个问题,我为此编写了一个mysql查询。虽然我很确定我编写的查询可以进一步优化,但它可以得到理想的结果。

SELECT *
FROM message AS M
NATURAL JOIN
  (SELECT M2.msgFrom,
          M2.msgTo,
          max(M2.msgDate) AS msgDate
   FROM message AS M2
   GROUP BY M2.msgFrom,
            M2.msgTo ) AS N
WHERE NOT EXISTS
( SELECT *
 FROM (
         (SELECT *
          FROM
            (SELECT M3.msgFrom,
                    M3.msgTo,
                    max(M3.msgDate) AS msgDate
             FROM message AS M3
             WHERE M3.msgFrom = 129
             GROUP BY M3.msgFrom,
                      M3.msgTo) AS send
          WHERE EXISTS
              ( SELECT *
               FROM
                 (SELECT M4.msgFrom,
                         M4.msgTo,
                         max(M4.msgDate) AS msgDate
                  FROM message AS M4
                  WHERE M4.msgTo = 129
                  GROUP BY M4.msgFrom,
                           M4.msgTo) AS rec
               WHERE rec.msgDate > send.msgDate
                 AND rec.msgFrom = send.msgTo))
       UNION
         (SELECT *
          FROM
            (SELECT M5.msgFrom,
                    M5.msgTo,
                    max(M5.msgDate) AS msgDate
             FROM message AS M5
             WHERE M5.msgTo = 129
             GROUP BY M5.msgFrom,
                      M5.msgTo) AS rec1
          WHERE EXISTS
              (SELECT *
               FROM
                 (SELECT M6.msgFrom,
                         M6.msgTo,
                         max(M6.msgDate) AS msgDate
                  FROM message AS M6
                  WHERE M6.msgFrom = 129
                  GROUP BY M6.msgFrom,
                           M6.msgTo) AS send1
               WHERE send1.msgDate >= rec1.msgDate
                 AND send1.msgTo = rec1.msgFrom)) )AS Q
 WHERE M.msgFrom = Q.msgFrom
   AND M.msgTo = Q.msgTo
   AND M.msgDate = Q.msgDate)
ORDER BY msgDate DESC;

post