gpt4 book ai didi

mysql - 限制在子查询内

转载 作者:可可西里 更新时间:2023-11-01 08:51:20 27 4
gpt4 key购买 nike

我有一个消息表,可以包含主要消息和子消息。我想要实现的是只获得前 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

当我取消限制时,我总是得到第一个结果。但是当我输入限制时,我得到空值。有任何想法吗?有什么建议吗?

最佳答案

SELECT * FROM messages WHERE id = 1
Union all
SELECT * FROM messages WHERE object_id=1
ORDER BY date_sent ASC LIMIT 2

关于mysql - 限制在子查询内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13635524/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com