我假設你基本上要由每個線程最後職位的25%,以後的操作是由您決定。
如果我是正確的,那麼這段代碼應該爲你工作(爲MS-SQL編寫的,應該可以移植到易源碼):
CREATE TABLE posts (
post_id INT,
thread_id INT
)
INSERT INTO posts(post_id, thread_id) VALUES (1, 1)
INSERT INTO posts(post_id, thread_id) VALUES (2, 2)
INSERT INTO posts(post_id, thread_id) VALUES (3, 2)
INSERT INTO posts(post_id, thread_id) VALUES (4, 3)
INSERT INTO posts(post_id, thread_id) VALUES (5, 3)
INSERT INTO posts(post_id, thread_id) VALUES (6, 3)
INSERT INTO posts(post_id, thread_id) VALUES (7, 3)
INSERT INTO posts(post_id, thread_id) VALUES (8, 3)
INSERT INTO posts(post_id, thread_id) VALUES (9, 3)
INSERT INTO posts(post_id, thread_id) VALUES (10, 3)
INSERT INTO posts(post_id, thread_id) VALUES (11, 3)
SELECT src.*
FROM (
SELECT post_number = (
SELECT 1 + COUNT(*)
FROM posts pp
WHERE p.post_id > pp.post_id
AND p.thread_id = pp.thread_id
),
post_id,
thread_id
FROM posts p
) src
JOIN (
SELECT thread_id, cnt = COUNT(*)
FROM posts
GROUP BY thread_id
) counts
ON src.thread_id = counts.thread_id
WHERE (CONVERT(FLOAT, src.post_number)/CONVERT(FLOAT, counts.cnt)) >= 0.75
請注意,這不是高性能的查詢,主要是由於子查詢獲取post_number。對於支持它的DBMS,它可以用OVER子句寫得更好。
你在posts表中有一個post_id列嗎?和解決方案應該只有sqlite? – pkmiec
@Dooh是的,我確實有一個post_id,任何解決方案都應該沒問題,只要它在SQLite中也可以。 – wrongusername