所以我有两张桌子,说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