2016-12-25 45 views
2

通知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 | 
| ...| ...  | 
+-----------------+ 

我想获得像计算器通知。

我想要针对特定​​用户的每个活动类型和object_url组合来查询上次通知(如果活动类型是注释,则为注释内容,否则为null)。

例如,我有3个artiles 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 

我怎样才能改变它得到最后?

+0

解释*不起作用*。你得到任何错误或产生错误的结果? –

+0

您的预期产出是多少? – denny

+0

@MahediSabuj我编辑了我的问题 –

回答

3

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

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 
+0

对不起,接受您的答案,却没有注意到您的查询给了我第一条评论。我如何改变它以获得最后的? –

+0

我hava编辑我的答案 – farhadamjady

+0

我试过了,但仍然有相同的结果 –