繁体   English   中英

SQL内部联接两个表

[英]SQL inner join two tables

我正在从SQL数据库导入数据,并将其与Angular.js绑定。 我不太了解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

您的语法有点错误。 在联接中,您需要指定两个表的关联方式。 改为这样做:

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

正确的语法是:

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,则应该学习SQL的基本语法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM