这是我的表结构:
// QandA
+----+------+-----------+-------------------+-------------------------------+-------------+
| id | type | author_id | title | content | question_id |
+----+------+-----------+-------------------+-------------------------------+-------------+
| 1 | 0 | 10 | title of question | content of question | NULL |
| 2 | 1 | 33 | NULL | content of answer | 1 |
| 3 | 0 | 51 | title of question | content of question | NULL |
| 4 | 1 | 10 | NULL | content of answer | 1 |
| 5 | 1 | 23 | NULL | content of answer | 3 |
+----+------+-----------+-------------------+-------------------------------+-------------+
-- type column: 0 is question, 1 is answer
-- title column: it is always NULL for answers
-- question_id column: it is NULL for questions and the id of its own question for answers
如果author_id
是问题的作者,我想设置蓝色背景颜色。像这样的事情:
所以我想做的就是检测答案的作者是否与问题的作者相同?我怎样才能做到这一点?
这是我的查询:
SELECT id, (author_id = {Id don't know}) AS set_highlight
FROM QandQ
WHERE id = :id OR question_id = :id
ORDER BY type ASC, id ASC
<小时/>
这是 $id = 1
的预期结果:
+----+---------------+
| id | set_highlight |
+----+---------------+
| 1 | 1 |
| 2 | 0 |
| 4 | 1 |
+----+---------------+
最佳答案
你可以使用这个
SELECT id,
(CASE WHEN author_id = (
SELECT author_id
FROM QandA
WHERE id = :id
AND type = 0)
THEN 1
ELSE 0 END) AS set_highlight
FROM QandA
WHERE id = :id OR question_id = :id
ORDER BY type ASC, id ASC;
关于mysql - 如何检查答案的作者与问题的作者是否相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42294452/