这里是我的表结构:如何加入null?
-- qanda (stands for questions and answers)
+----+---------+-----------------------------------------------+--------------+
| id | title | content | question_id |
+----+---------+-----------------------------------------------+--------------+
| 1 | title1 | this is a question | NULL |
| 2 | NULL | this is an answer | 1 |
| 3 | NULL | this is another answer | 1 |
| 4 | title2 | this is another question | NULL |
| 5 | NULL | this is an answer for the second question | 4 |
| 6 | NULL | this is another answer for the first question | 1 |
+----+---------+-----------------------------------------------+--------------+
我知道,这将是更好,如果我保持两个不同的表中的问题和答案。但现在我只是想了解JOIN
在这种情况下的工作原理。
我有一个ID为qanda
表,我总是想要一个标题。该ID可能是问题的ID或答案的ID。我怎样才能做到这一点?
我想是这样的:
SELECT t1.title
FROM qanda t1
INNER JOIN qanda t2
ON t1.id = t2.question_id
WHERE t1.id = :id
我的查询没有匹配。这里是预期结果的一些样品:
-- :id = 1
+--------+
| title1 |
+--------+
-- :id = 2
+--------+
| title1 |
+--------+
-- :id = 4
+--------+
| title2 |
+--------+
-- :id = 5
+--------+
| title2 |
+--------+
-- :id = 6
+--------+
| title1 |
+--------+
创建表格的脚本 – etsa
@etsa请问您需要什么?表格结构在我的问题中非常清楚。 –
这对你来说很清楚......不适合我......如果你需要帮助,请发布要求 – etsa