关于ORDER BY - MySQL

时间:2017-03-17 18:08:49

标签: mysql sql group-by sql-order-by

我正在尝试与其他用户进行最后一次用户的聊天:

这些是我的表格:

表格名为“user_data”

---------------------------
| id | owner |    name    |
---------------------------  
|  1 |  120  |  "Nicolas" |   
|  2 |  140  |   "Angel"  |
|  3 |  200  |   "Isaac"  |
---------------------------

表格名为“private_messages”

------------------------------------------------------------------------
| id | ownerID | toUserID | chatID | message             | date        |
------------------------------------------------------------------------
|  1 |   120   |   140    |   10   | "Hi"                | 1489777023  |
|  2 |   120   |   140    |   10   | "How are you?"      | 1489777024  |
|  3 |   120   |   140    |   10   | "I miss u"          | 1489777025  |
|  4 |   140   |   120    |   10   | "Viewed."           | 1489777123  |
|  5 |   200   |   120    |   20   | "Hey! What happen?" | 1489777111  |
|  6 |   200   |   120    |   20   | "We were friends."  | 1489777300  |
------------------------------------------------------------------------

我想获取当前登录用户的每个聊天的最后一条消息。所以,我做了下一个:首先,我得到所有的聊天,其中我的ID与“ownerID”和“toUserID”相同,然后我将这些数据与“chatID”组合在一起。

我的查询:

SELECT
    _d.chatID, 
    ANY_VALUE( _d.name) AS name,
    ANY_VALUE( _d.message) AS message,
    ANY_VALUE( _d.date) AS date 
FROM (
    SELECT 
        user_data.name,
        private_messages.message,
        private_messages.date,
        private_messages.chatID 
        FROM private_messages 

        INNER JOIN user_data ON private_messages.owner = user_data.owner 

        WHERE private_messages.owner = "120" 
              OR private_messages.touser = "120"

        ORDER BY private_messages.date DESC) AS _d
GROUP BY _d.chatID

问题是这显示了第一条消息,而不是最后一条消息,例如,此查询为我提供了下一条数据:

---------------------------------------------------
| name      | message               | date        |
---------------------------------------------------
| "Nicolas" |   "Hi"                | 1489777023  | <-- First message
| "Isaac"   |   "Hey! What happen?" | 1489777111  | <-- First message
---------------------------------------------------

我希望这个结果:

------------------------------------------------
| name    | message              | date        |
------------------------------------------------
| "Angel" |  "Viewed."           | 1489777123  | <-- Last message
| "Isaac" |  "We were friends."  | 1489777300  | <-- Last message
------------------------------------------------

3 个答案:

答案 0 :(得分:0)

如果private_messages.idAUTO_INCREMENT PRIMARY KEY,您可以使用此功能:

select m.chatID, ud.name, pm.message, pm.date
from (
    select max(id) as id
    from private_messages
    where 120 in (ownerID, touserID)
    group by chatID
) sub
join private_messages pm on pm.id = sub.id
join user_data ud on ud.owner = pm.owner

答案 1 :(得分:0)

SELECT B.name,
       A.message,
       A.date1
  FROM     
 ( 
 SELECT MAX( _d.ownerID ) AS ownerID, MAX( _d.message ) AS message,MAX( _d.date1 ) AS date1
    FROM 
       (
         SELECT                
                private_messages.ownerID,
                private_messages.message,
                private_messages.date1,
                private_messages.chatID 
           FROM private_messages 
        INNER JOIN
                user_data
             ON private_messages.ownerid = user_data.owner1 
          WHERE private_messages.ownerid = 120
             OR private_messages.touserid = 120
       ORDER BY private_messages.date1 DESC
       ) AS _d
GROUP BY _d.chatID
) A
INNER JOIN 
user_data B
ON A.ownerID = B.owner1
;

答案 2 :(得分:0)

首先,您只按聊天ID选择聊天ID和日期和分组:

select chatID, max(date) as maxdate
from private_messages
where ownerID = 120
or toUserID = 120
group by chatID

中期结果将是

chatID    maxdate
------    -------
10        1489777123
20        1489777300

然后将此结果与您必须获得的其他两个表格结合起来 (maxdate = date

请注意,必须首先执行select max(date) / group by chatID以摆脱ANY_VALUE构造,而您无法预测从中选择的值。