鉴于以下架构,成员可以共享帖子的所有权:
member: id, name, ...
post_owner: member_id, post_id
post: id, created, ...
查找过去90天内没有发帖的用户,最有效的查询是什么。
我想到了这一点,但似乎效率低下:
select *
from member
where member_id not in (
select member_id
from post p
join post_owner po on po.post_id = p.id
where created > subdate(now(), 90))
假设post(created)
和所有外键都有索引。
答案 0 :(得分:1)
这是怎么回事?
SELECT m.id, m.name
From post p
INNER JOIN post_owner po ON po.post_id = p.id AND
RIGHT JOIN member m ON m.id = po.member_id
where DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY) <= created
答案 1 :(得分:1)
SELECT m.id, m.name
FROM member m
LEFT JOIN post_owner po ON m.id = po.member_id
LEFT JOIN post p ON po.post_id = p.id AND p.created >= DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY)
WHERE p.id IS NULL
提示:将p.created
与常量进行比较,以便使用索引。
答案 2 :(得分:0)
试试这个:
SELECT m.id, m.name
FROM member m
LEFT JOIN post_owner po ON m.id = po.member_id
LEFT JOIN post p ON po.post_id = p.id AND DATEDIFF(CURRENT_DATE(), p.created) <= 90
WHERE p.id IS NULL