这有点是Database Modeling: Facebook like messages的延续。
我正在尝试模拟类似于Facebook消息的内容。在Facebook中,用户可以在墙上,发布的媒体(例如照片,视频)上发表评论,或者互相发送电子邮件。下表试图表示:
===========================
message
===========================
- message_id (PK)
- parent_message_id (FK)
- profile_id (FK, referring to who posted the message)
- message
- subject (applicable only for emails)
- timestamp
===========================
wall_message
===========================
- message_id (FK)
- profile_id (FK, referring to who received the message/owner of wall)
===========================
media_message
===========================
- message_id (FK)
- media_id (FK, referring to the specific photo, video, etc.)
===========================
email_message
===========================
- message_id (FK)
- profile_id (FK, referring to who received the message)
几个问题:
答案 0 :(得分:-1)
尝试以下(未经测试):
SELECT TOP 4 *
FROM message INNER JOIN wall_message ON message.message_id = wall_message.message_id
WHERE wall_message.profile_id = @prof_id
ORDER BY message.timestamp DESC