我如何过滤属于已删除帖子的答案搜索?

mpbci0fu  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(309)

我有一张这样的table:

// questions_and_answers
+----+---------------+------------------------+---------+---------+
| id |    subject    |           body         | related | deleted |
+----+---------------+------------------------+---------+---------+
| 1  | subject1      | question1              | NULL    | 0       |
| 2  |               | answer1                | 1       | 0       |
| 3  | subject2      | question2              | NULL    | 0       |
| 4  |               | answer2                | 3       | 1       |
| 5  |               | answer3                | 3       | 0       |
| 6  | subject3      | question3              | NULL    | 1       |
| 7  |               | answer4                | 6       | 0       |
+----+---------------+------------------------+---------+---------+
-- related column contains either NULL for questions or the id of its question for answers

从上表中可以看出,两者 answer2 以及 question3 已删除。所以我想在搜索中过滤它们。关键是,我想过滤 answer4 任何一个。因为它属于删除的问题( question3 ). 你知道我怎么做这种过滤吗?
以下是我当前的查询:

SELECT qa.*
FROM questions_and_answers AS qa
WHERE MATCH(subject,body) AGAINST (:entry)
  AND deleted = 0
ORDER BY id DESC
LIMIT :page, 10;

如您所见,它只过滤删除的帖子。它不关心过滤属于已删除问题的答案。

gywdnpxw

gywdnpxw1#

问题和答案可能分在不同的表格里;这样你就不会有这个问题了。在这种情况下,您可能会在一对多关系中使用一个简单的连接。你可以用CTE来模拟:

with q as (
    select * from questions_and_answers
    where related is null and deleted = 0
), a as (
    select * from questions_and_answers
    where related is not null and deleted = 0
)
select *
from q left outer join a on a.related = q.related
order by q.id, a.id;

如果答案显示为自己的行是绝对必要的:

with qa as (
    select *, max(deleted) over (partition by coalesce(related, id)) as deleted
    from question_and_answers
)
select * from qa where deleted = 0
order by coalesce(related, id), id;

我只是意识到你使用的是mysql,所以你可能没有可用的cte和/或分析函数。希望这对你还是有用的。

6l7fqoea

6l7fqoea2#

你可以再加一个 AND 运算符使用相关子查询Assert已删除父项的存在,并修改您的条件以获得更优化的查询:

SELECT qa.*
FROM questions_and_answers AS qa
WHERE MATCH(subject,body) AGAINST (:entry)
  AND deleted = 0
  AND (qa.related IS NULL OR
    NOT EXISTS (SELECT 1
      FROM questions_and_answers qaa
      WHERE qa.related = qaa.id AND qaa.deleted = 1)
  )
ORDER BY id DESC
LIMIT :page, 10;
lmvvr0a8

lmvvr0a83#

如果只有一级关系——对于任何一行r,存在另一行r',那么r' .related =r .id ,右。 related IS NULL --你可以把问题和答案连接起来,然后检查问题的答案 deleted 或者使用 NOT EXISTS 检查是否不存在已删除的父记录。 LEFT JOIN 变体:

SELECT qa1.*
       FROM questions_and_answers qa1
            LEFT JOIN questions_and_answers qa2
                      ON qa2.id = qa1.related
       WHERE MATCH(qa1.subject, qa1.body) AGAINST (:entry)
             AND qa1.deleted = 0
             AND coalesce(qa2.deleted, 0) = 0
       ORDER BY qa1.id DESC
       LIMIT :page, 10;
``` `NOT EXISTS` 变体:

SELECT qa1.*
FROM questions_and_answers qa1
WHERE MATCH(qa1.subject, qa1.body) AGAINST (:entry)
AND qa1.deleted = 0
AND NOT EXISTS (SELECT *
FROM questions_and_answers qa2
WHERE qa2.id = qa1.related
AND qa2.deleted = 1)
ORDER BY qa1.id DESC
LIMIT :page, 10;

相关问题