所以我有兩張桌子,說comment_table和post_table,加入兩個表的列
comment_table:
link_id body
t3_100 people on StackOverflow are smart
t3_100 StackOverflow is a good place to raise questions
t3_101 where can I learn sql
t3_102 Happy Eastern
t3_102 where did my bunny go?
post_table
id title
100 Thought on StackOverflow
101 sql beginner
102 Eastern
105 Title that has no comments
「link_id」 是由串聯 't3_' +來自post_table的ID。我想要的是兩個「id」加入這兩個表。
期望輸出
id title link_id body
100 Thought on StackOverflow t3_100 people on StackOverflow are smart
100 Thought on StackOverflow t3_100 StackOverflow is a good place to raise questions
101 sql beginner t3_101 where can I learn sql
102 Eastern t3_102 Happy Eastern
102 Eastern t3_102 where did my bunny go?
105 Title that has no comments t3_105 NULL
這裏是腳本我有,
SELECT PT.ID, PT.title, CT.link_id, CT.body
FROM post_table as PT
LEFT OUTER JOIN comment_table as CT
ON PT.ID = CT.concat('t3_', link_id)
它語法錯誤,你怎麼認爲我可以解決這個問題,以獲得預期的輸出?
錯誤消息應該告訴你在句法錯誤所在。編輯你的問題,並添加完整的錯誤信息。 –
爲什麼不直接在'link_id'中存儲整數?你這樣做的方式,你不能定義一個外鍵。如果您需要以其他方式加入,您將無法爲連接使用索引。 –
在任何情況下,您都有兩個表別名(PT和CT)切換到腳本的最後一行,這意味着您必須連接PT.ID而不是CT.link_id。嘗試先改變它。 Luc – luc