根据最后一个条目获取列值(非空)

时间:2013-11-19 15:57:06

标签: mysql sql group-by max

我在MySQL中有一个表可以保存对话。这些对话由消息组成。单个对话如下表所示。

ImportanceeId只会被设置。我想从表中获得的是对话中的最后一条消息(messageId = 4),但最后一组importance和最后一组eId

所以,从这张表

+----------------+-----------+----------+----------+------------+-------+---------+
| conversationId | messageId | time     | status   | importance | eId   | message |
+----------------+-----------+----------+----------+------------+-------+---------+
| 25             | 4         | 11:00:00 | feedback | NULL       | NULL  | d..     |
+----------------+-----------+----------+----------+------------+-------+---------+
| 25             | 3         | 10:00:00 | open     | MEDIUM     | NULL  | c..     |
+----------------+-----------+----------+----------+------------+-------+---------+
| 25             | 2         | 09:00:00 | feedback | NULL       | 123   | b...    |
+----------------+-----------+----------+----------+------------+-------+---------+
| 25             | 1         | 08:00:00 | open     | HIGH       | NULL  | a...    |
+----------------+-----------+----------+----------+------------+-------+---------+

我需要得到这个结果

+----------------+-----------+----------+----------+------------+-------+---------+
| conversationId | messageId | time     | status   | importance | eId   | message |
+----------------+-----------+----------+----------+------------+-------+---------+
| 25             | 4         | 11:00:00 | feedback | MEDIUM     | 123   | d..     |
+----------------+-----------+----------+----------+------------+-------+---------+

我无法让查询起作用。 任何帮助都会得到满足。感谢。

3 个答案:

答案 0 :(得分:0)

这个不一定是最好的解决方案,因为我必须在我的本地SQL Server中创建它(尝试时没有安装MySQL的SQL小提琴),也是一个“快速和脏”的查询(意味着:可能对你的问题是一个更好的解决方案),但我仍然热衷于帮助我发布它 如果您碰巧等待社区的另一个可能更好的解决方案,我不会感到被冒犯:)这就是我想说的话:)

SELECT TOP 1 conversationId,messageId,RecTime,ConvStatus,
       (
            SELECT TOP 1 importance
            FROM Conversations
            WHERE importance IS NOT NULL
            ORDER BY messageId DESC
       ) AS importance,
       (
            SELECT TOP 1 eId
            FROM Conversations
            WHERE eId IS NOT NULL
            ORDER BY messageId DESC
       ) AS eId,
UsrMessage
FROM Conversations
ORDER BY messageId DESC
GO

(不要忘记您可能需要更改“格式化项目”才能使其在MySQL中运行)

答案 1 :(得分:0)

如果表中有多个conversationId,并且您希望同时获得所有conversationId的所需结果,那么我认为您需要在子查询中加入3个子查询。类似于下面的3:

SELECT messages.conversationId, messages.messageId, messages.time, messages.status, messages.message
FROM messages
JOIN  (
  SELECT conversationId, MAX(messageId) as messageId
  FROM messages
  GROUP BY conversationId) as m2
ON (messages.messageId = m2.messageId);


SELECT messages.conversationId, messages.importance
FROM messages
JOIN  (
  SELECT conversationId, MAX(messageId) as messageId
  FROM messages
  WHERE importance IS NOT NULL     
  GROUP BY conversationId) as m3
ON (messages.messageId = m3.messageId);

SELECT messages.conversationId, messages.eId
FROM messages
JOIN (
  SELECT conversationId, MAX(messageId) as messageId
  FROM messages
  WHERE eId IS NOT NULL
  GROUP BY conversationId) as m4
ON (messages.messageId = m4.messageId);

JOIN看起来像这样:

SELECT 
main.conversationId, 
main.messageId, 
main.time, 
main.status, 
importance.importance, 
eId.eId, 
main.message
FROM (
  SELECT messages.conversationId, messages.messageId, messages.time, messages.status, messages.message
  FROM messages
  JOIN  (
    SELECT conversationId, MAX(messageId) AS messageId
    FROM messages
    GROUP BY conversationId) AS m2
    ON (messages.messageId = m2.messageId)
        ) AS main
JOIN (
  SELECT messages.conversationId, messages.importance
  FROM messages
  JOIN  (
    SELECT conversationId, MAX(messageId) AS messageId
    FROM messages
    WHERE importance IS NOT NULL
    GROUP BY conversationId) AS m3
    ON (messages.messageId = m3.messageId)
        ) AS importance
JOIN (
  SELECT messages.conversationId, messages.eId
  FROM messages
  JOIN (
    SELECT conversationId, MAX(messageId) AS messageId
    FROM messages
    WHERE eId IS NOT NULL
    GROUP BY conversationId) AS m4
    ON (messages.messageId = m4.messageId)
      ) AS eId
ON (
  main.conversationId = importance.conversationId
  AND main.conversationId = eId.conversationId
    );

这是一个sqlfiddle:http://sqlfiddle.com/#!2/857aa/38。这假设messageId是唯一的。如果它不是唯一的,那么您需要加入messageId AND conversationId。

答案 2 :(得分:0)

也许有点老了,但应该这样做:

SELECT c.conversationId as convId, max(c.messageId), t.time, s.status, i.importance, e.eId, c.message
  FROM convs c,
   (SELECT conversationId, max(time) AS time FROM convs) t,
   (SELECT conversationId, status FROM convs WHERE status IS NOT NULL ORDER BY messageId DESC) s,
   (SELECT conversationId, importance FROM convs WHERE importance IS NOT NULL ORDER BY messageId DESC) i,
   (SELECT conversationId, eId FROM convs WHERE eId IS NOT NULL ORDER BY messageId DESC) e
 WHERE 1=1
   AND t.conversationId = c.conversationId
   AND s.conversationId = c.conversationId
   AND i.conversationId = c.conversationId
   AND e.conversationId = c.conversationId
 GROUP BY c.conversationId

SQL Fiddle demo