這裏是我的表結構:如何加入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