2014-09-21 128 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使用它。