2016-12-25 53 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

我試過了,但仍然有相同的結果 –