我不知道如何編寫獲取最後一條記錄的SQL語法(根據最近的帖子,沒有回覆)。獲取每個記錄組的最後一條記錄
我的表
+-------------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-----------------------+------+-----+---------+----------------+
| notification_id | mediumint(8) unsigned | NO | PRI | NULL | auto_increment |
| user_id | mediumint(8) unsigned | NO | | NULL | |
| notification_msg | text | NO | | NULL | |
| notification_date | int(11) unsigned | NO | | NULL | |
| private_message | tinyint(1) unsigned | NO | | 0 | |
| has_replied | tinyint(1) unsigned | NO | | 0 | |
| reply_id | mediumint(8) unsigned | NO | | 0 | |
+-------------------+-----------------------+------+-----+---------+----------------+
基本上每個線程的通知,它應該得到每個通知記錄中的最後一條記錄,並檢查是否has_replied
是0
,如果是0,那麼它應該返回它使PHP可以讀取是否有沒有回覆的通知。因此,它應該像這樣返回(僞):
+--------------+-----+-----+
| username | 1 | 4 |
| username2 | 0 | 2 |
+--------------+-----+-----+
其中第二列表示是否回覆上一篇文章。
我現在的SQL語法(的作品,但沒有得到最後的記錄,如果它回答):
SELECT n.*,
m.user_id,
m.username
FROM notifications n
INNER JOIN members m ON n.user_id = m.user_id
WHERE private_message = 1
AND reply_id = 0
ORDER BY has_replied ASC,
notification_date DESC
+1。我喜歡桌子格式':)' – 2011-05-09 20:39:24
很高興你喜歡它。 – MacMac 2011-05-09 20:39:55
僞輸出中的第三列是什麼? – 2011-05-09 20:47:03