SQL获取某些用户类型的最后一个回复

时间:2013-11-12 16:22:22

标签: mysql sql

我需要的目标是获取最后一个回复来自用户而不是管理员的票据,最后一个回复是10天或更长时间。我目前有一个SQL获得最后的回复,但我很难从管理员过滤用户。我当前的SQL是:

SELECT
    t.tid, tr.reply_date, tr.rid, u.display_name, u.user_type
FROM ticket t
JOIN (
    SELECT 
        tr_last.tid, tr_last.reply_date, tr_last.rid, tr_last.uid
    FROM ticket_reply tr_last 
    ORDER BY tr_last.reply_date DESC
) tr ON tr.tid = t.tid
LEFT JOIN user u ON u.uid = tr.uid
WHERE 
    t.status != "Closed"
    AND DATE_SUB(CURDATE(), INTERVAL 10 DAY) >= tr.reply_date
GROUP BY t.tid;

返回类似于:

的内容
+-------+---------------------+-------+-----------------------------+-----------+
| tid   | reply_date          | rid   | display_name                | user_type |
+-------+---------------------+-------+-----------------------------+-----------+
|  6784 | 2012-03-27 04:11:52 | 40061 | (hidden)                    | admin     |
|  8094 | 2013-01-18 21:13:56 | 65037 | (hidden)                    | admin     |
|  8165 | 2012-09-10 14:59:17 | 52956 | (hidden)                    | admin     |
|  8171 | 2012-06-18 19:05:19 | 46527 | (hidden)                    | user      |
|  8313 | 2012-08-16 23:05:51 | 51362 | (hidden)                    | admin     |
|  8436 | 2012-08-16 23:20:20 | 51368 | (hidden)                    | admin     |
|  8453 | 2012-06-26 23:41:37 | 47247 | (hidden)                    | admin     |
|  8712 | 2012-11-22 10:33:05 | 59697 | (hidden)                    | user      |
|  8911 | 2013-09-24 00:52:27 | 89910 | (hidden)                    | user      |
|  8949 | 2013-09-02 16:29:22 | 87718 | (hidden)                    | user      |
+-------+---------------------+-------+-----------------------------+-----------+

如果我在u.user_type = "user"子句中添加WHERE,则会更改其检索到的tr中的哪一行,以便现在6784 tid行中的用户显示用户不是管理员,但该票证的最后一个回复实际上是管理员。所以这个:

SELECT
    t.tid, tr.reply_date, tr.rid, u.display_name, u.user_type
FROM ticket t
JOIN (
    SELECT 
        tr_last.tid, tr_last.reply_date, tr_last.rid, tr_last.uid
    FROM ticket_reply tr_last 
    ORDER BY tr_last.reply_date DESC
) tr ON tr.tid = t.tid
LEFT JOIN user u ON u.uid = tr.uid
WHERE 
    t.status != "Closed"
    AND DATE_SUB(CURDATE(), INTERVAL 10 DAY) >= tr.reply_date
    AND u.user_type = "user"
GROUP BY t.tid;

现在返回:

+-------+---------------------+-------+-----------------------------+-----------+
| tid   | reply_date          | rid   | display_name                | user_type |
+-------+---------------------+-------+-----------------------------+-----------+
|  6784 | 2012-03-26 23:19:52 | 40051 | (hidden)                    | user      |
|  8094 | 2013-01-18 18:53:33 | 65016 | (hidden)                    | user      |
|  8165 | 2012-09-07 01:17:28 | 52831 | (hidden)                    | user      |
|  8171 | 2012-06-18 19:05:19 | 46527 | (hidden)                    | user      |
|  8313 | 2012-08-09 18:37:35 | 50776 | (hidden)                    | user      |
|  8436 | 2012-08-16 23:01:28 | 51359 | (hidden)                    | user      |
|  8453 | 2012-06-21 16:29:24 | 46843 | (hidden)                    | user      |
|  8712 | 2012-11-22 10:33:05 | 59697 | (hidden)                    | user      |
|  8911 | 2013-09-24 00:52:27 | 89910 | (hidden)                    | user      |
|  8949 | 2013-09-02 16:29:22 | 87718 | (hidden)                    | user      |
+-------+---------------------+-------+-----------------------------+-----------+

您可以注意到rid列值也不同。

所以我的问题是,如何为此正确地返回用户?

1 个答案:

答案 0 :(得分:2)

你没有在任何地方得到最后的回复。在子查询中,您只是对值进行排序。

尝试这样的事情:

SELECT t.tid, tr.reply_date, tr.rid, u.display_name, u.user_type
FROM ticket t
INNER JOIN (
  SELECT tr_last.tid, tr_last.reply_date, tr_last.rid, tr_last.uid
  FROM ticket_reply tr_last
  INNER JOIN (
    SELECT tid, max(reply_date)
    FROM ticket_reply
    GROUP BY tid
    ) tr2 ON tr2.tid = tr_last.tid AND tr_last.reply_date = tr2.reply_date
  ) tr ON tr.tid = t.tid
LEFT JOIN user u ON u.uid = tr.uid AND u.user_type = "user"
WHERE t.STATUS != "Closed"
  AND DATE_SUB(CURDATE(), INTERVAL 10 DAY) >= tr.reply_date;

内部查询返回tid及其MAX(日期),并提供最新的回复信息。

(我没有测试过这个,但看起来像你在找什么)。