我有一个选择第n个结果的查询(在这种情况下是第10个)我只想使用第n个结果的reply_chunk_id并计算其他类似的结果来查看总共有多少个具有相同id的结果。有人知道如何在一个查询中执行此操作吗?
SELECT reply_chunk_id,message
FROM (
SELECT *
FROM messages
ORDER BY timestamp ASC
LIMIT 10
) AS tbl WHERE topic_id=?
ORDER BY timestamp DESC
LIMIT 1
答案 0 :(得分:3)
您可以按
选择第10行SELECT reply_chunk_id FROM messages ORDER BY timestamp ASC LIMIT 9,1
所以
SELECT COUNT(*) FROM messages
WHERE reply_chunk_id = (SELECT reply_chunk_id FROM messages
ORDER BY timestamp ASC LIMIT 9,1)
AND topic_id = ?
答案 1 :(得分:0)
可能使用LIMIT 1 OFFSET 9。不确定这是在MySQL中构建的。
答案 2 :(得分:0)
SELECT COUNT(1)
FROM messages
WHERE reply_chunk_id =
(SELECT MIN(reply_chunk_id)
FROM messages
WHERE timestamp =
(SELECT MAX(timestamp)
FROM (SELECT timestamp
FROM messages
ORDER BY timestamp ASC
LIMIT 10)))