我有一个可以包含主要消息以及子消息的消息表。我想要实现的只是获得前 2 个最近的子消息。
SELECT message.id, message.date_sent,message.object_id, message.content,
SubMessage1.content, SubMessage1.id, SubMessage1.date_sent,
SubMessage2.content, SubMessage2.id, SubMessage2.date_sent
FROM messages As Message
LEFT
OUTER
JOIN ( SELECT messages.object_id as object_id, messages.content as content,
messages.id as id , messages.date_sent as date_sent
FROM messages
ORDER
BY messages.date_sent ASC LIMIT 1,1
) as SubMessage1
ON Message.id = SubMessage1.object_id
LEFT
OUTER
JOIN ( SELECT messages.object_id as object_id, messages.content as content,
messages.id as id , messages.date_sent as date_sent
FROM messages
ORDER
BY messages.date_sent ASC LIMIT 2,1
) as SubMessage2
ON Message.id = SubMessage2.object_id
WHERE Message.id = 1
当我删除限制时,我总是得到第一个结果。但是当我输入限制时,我得到空值。有任何想法吗?建议?