Mysql查询stackoverflow上的通知的最佳方法

时间:2016-12-25 06:43:16

标签: mysql

我有4张桌子:

comments
+----+-----------+--------------+-------+
| id | content   | user_id | article_id |
+----+-----------+--------------+-------+
| 1  | Comment 1 | 2       | 5          |
| 2  | Comment 2 | 5       | 3          |
| 3  | Comment 3 | 1       | 6          |
| 4  | Comment 4 | 6       | 8          |
| 5  | Comment 5 | 1       | 6          |
| ...| ...       | ...     | ...        |
+----------------+---------+------------+

votes
+----+----------+--------------+---+
| id | type | user_id | article_id |
+----+----------+--------------+---+
| 1  | 1    | 2       | 5          |
| 2  | 1    | 3       | 3          |
| 3  | 0    | 1       | 6          |
| 4  | 1    | 7       | 4          |
| 5  | 0    | 9       | 4          |
| 6  | 0    | 1       | 6          |
| ...| ...  | ...     | ...        |
+------------+----------+----------+

notifications (object_id is the id of the vote|comment)
+----+----------+--------------+-------------+-------------+--------------+
| id | object_url| object_id |activitytype_id|  sender_id  | recipient_id | 
+----+----------+------------+---------------+-------------+--------------+
| 1  | /../../.. | 1         | 2             |       2     |       6      | 
| 2  | /../../.. | 2         | 2             |       3     |       2      | 
| 3  | /../../.. | 1         | 1             |       2     |       7      | 
| 3  | /../../.. | 2         | 1             |       5     |       2      | 
| 3  | /../../.. | 3         | 1             |       1     |       3      | 
| 3  | /../../.. | 3         | 3             |       1     |       2      | 
| 3  | /../../.. | 4         | 2             |       7     |       8      |
| 3  | /../../.. | 5         | 3             |       9     |       1      |
| 3  | /../../.. | 6         | 3             |       1     |       5      | 
| ...| ...       | ...       | ...           |             |              | 
+----+-----------+-----------+---------------+-------------+--------------+

activitytypes
+----+------------+
| id |    label   |
+----+------------+
| 1  | comment    |
| 2  | vote up    |
| 3  | vote down  |
| ...| ...        |
+-----------------+

我想收到有关stackoverflow的通知。

对于特定用户的每个activitytype和object_url组合,我想查询最后一个通知(如果活动类型是注释,则为评论内容,否则为null)。

例如,我有3个艺术品A,B和C,它们都有3个评论,4个投票和2个投票。如何获得每篇文章的最后评论,投票和投票?

我试过这个问题:

SELECT n.id, n.object_url , n.object_id, n.activitytype_id, IF(n.activitytypeId = 1, 
(SELECT content FROM comments WHERE id=n.object_id), null) AS activitycontent     
FROM notifications n WHERE n.recipient_id =1
GROUP BY n.activitytype_id,n.object_url 
ORDER BY n.id DESC

但它不起作用。有人可以帮忙吗?

修改 在farhadamjady的回答中的以下查询给了我第一个评论:

SELECT
n.id,
n.object_url,
n.object_id,
n.activitytype_id,
cm.content AS activitycontent
FROM
  notifications n
LEFT OUTER JOIN `COMMENT` AS cm ON cm.id = n.object_id and n.activitytypeId  = 1
WHERE
  n.recipient_id = 1
GROUP BY
  n.activitytype_id,
  n.object_url
HAVING MAX(cm.id)
ORDER BY
  n.id DESC

如何更改它以获取最后一个?

1 个答案:

答案 0 :(得分:4)

你应该像这样使用左外连接:

SELECT
n.id,
n.object_url,
n.object_id,
n.activitytype_id,
cm.content AS activitycontent
FROM
  notifications n
 LEFT OUTER JOIN `COMMENT` AS cm ON cm.id = n.object_id and n.activitytypeId  = 1
WHERE
  n.recipient_id = 1
GROUP BY
   n.activitytype_id,
   n.object_url
 HAVING MAX(cm.id)
ORDER BY
  n.id DESC