我有这张桌子:
// QandA
+----+-----------+-----------------------+-----------+------+---------+
| id | title | content | related | type | deleted |
+----+-----------+-----------------------+-----------+------+---------+
| 1 | question1 | content of question1 | 1 | 0 | 0 |
| 2 | answer1-1 | content of answer1-1 | 1 | 1 | 0 |
| 3 | question2 | content of question2 | 3 | 0 | 0 |
| 4 | answer1-2 | content of answer1-2 | 1 | 1 | 0 |
| 5 | answer2-1 | content of answer2-1 | 3 | 1 | 0 |
+----+-----------+-----------------------+-----------+------+---------+
related
列包含自己所有答案的问题ID。type
列的问题为0
,问题为1
。deleted
列为0
,当问题\答案未被删除时,1
会被删除。这也是我的疑问:
SELECT * FROM QandA WHERE related = :id AND deleted = 0
我的问题:如何实施此条件:
如果deleted
为1
那么:
type
是0
(这是一个问题)则选择type
是1
(这是答案)那么只有不选择该行答案 0 :(得分:1)
我想你想要排除已删除问题的所有答案:
1)删除应为0
2)它应该是一个问题或(如果是答案)它不应该属于已删除的问题
SELECT * FROM QandA qa
WHERE
related = :id
AND deleted = 0
AND (
type = 0
OR NOT EXISTS (
SELECT * FROM QandA q WHERE q.id = qa.related AND q.deleted
)
)