2014-09-21 131 views
-1

我正在从SQL数据库导入数据并将其与Angular.js绑定。我不太了解SQL,并且遇到了一些问题。SQL内部连接两张表

我想要做的是让相关图像与帖子一起出现。这是我迄今为止所提出的。

select posts.id, posts.name, posts.description, posts.date, posts.email 
from posts Inner Join images on images.id, images.post_id, images.image 
order by posts.date desc 

架构是

职位(表):

id(pk), name, description, date, email 

图片(表):

id, post_id(fk), image 
+3

那么,你的问题是什么? – Mureinik 2014-09-21 18:48:35

+0

你有什么问题? – GolezTrol 2014-09-21 18:48:49

回答

2

你有语法有点不对劲。在连接中,您需要指定两个表的关联方式。做到这一点,而不是:

select 
posts.id, posts.name, posts.description, posts.date, posts.email, image.image 
from posts 
Inner Join images on images.post_id = posts.id 
order by posts.date desc 
+0

是的,它是一个语法错误。这个作品谢谢你。 – imnew123 2014-09-21 18:58:50

1

正确的语法是:

select p.id, p.name, p.description, p.date, p.email, images.id, i.image 
from posts p Inner Join 
    images i 
    on i.post_id = p.id 
order by p.date desc; 

你应该学习SQL的基本语法,如果你要effecctively使用它。