简体   繁体   English

从另一个表获取记录

[英]Getting a record from another table

I'm trying to read all of my data, but there is one record that is in another table. 我正在尝试读取所有数据,但是另一张表中有一条记录。 Can I read it together with my other records in one query? 我可以在一个查询中将其与其他记录一起阅读吗?

 $sql_query="SELECT * FROM blog_posts ORDER BY id DESC";

// table to display results here // modify here 
echo '<h1>'.$rows["title"].'</p></h1>';
echo '<p> '.$rows["post"].'</p>';
echo '<p><span class="style1">Door</span>: '.$rows["first_name"].' ';
echo '<p>Geplaatst op</span>: '.$rows["date_posted"].'</p>';                           
echo "<BR>";

The record: first_name is comming from the table: people. 记录:first_name来自表:people。 The rest is all from blog_posts. 其余的全部来自blog_posts。

Use LEFT JOIN in your query like this 像这样在查询中使用LEFT JOIN

SELECT * FROM blog_posts
LEFT JOIN people
ON blog_posts.{USERIDCOLUMNNAME}=people.{USERIDCOLUMNNAME}
ORDER BY blog_posts.id DESC; 

Or like Raul posted in his comment define every column: 或像劳尔(Raul)在其评论中发布的那样,定义每列:

SELECT people.name as name, blog_posts.title as title, blog_posts.post as post FROM blog_posts,people WHERE blog_posts.people_id=people.id ORDER BY id DESC 

Call me crazy, but it looks like first_name is a field in another table and not an entire record. 叫我疯了,但看起来first_name是另一个表中的字段,而不是整个记录。 Therefore I think the approach you're looking for is to join your blog_posts table with your people table. 因此,我认为您正在寻找的方法是将blog_posts表与people表一起加入。 How you go about that, though completely depends on the relationship: one-to-one, one-to-many, or many-to-many. 尽管这完全取决于关系:一对一,一对多或多对多。

If it's one to one (which it probably isn't), then you most likely have a foreign key to the other table in both tables (ie there is a blog_post.people_id field and a people.blog_post_id field). 如果是一对一的(可能不是),那么您很可能在两个表中都有指向另一个表的外键(即,有一个blog_post.people_id字段和一个people.blog_post_id字段)。 If this is the case, then it doesn't matter which way you perform the join. 如果是这种情况,那么执行连接的方式都没关系。 Since you're already selecting from the blog_posts table, then it could look like this: 由于您已经从blog_posts表中进行选择,因此看起来可能像这样:

SELECT * FROM blog_posts
    INNER JOIN people ON blog_posts.people_id = people.id

Note that using the * in this manner when selecting will select all fields from both tables (not what you want, most likely). 请注意,在选择时以这种方式使用*会从两个表中选择所有字段(很有可能不是您想要的)。

For one-to-many, there is only a single foreign key on one of your tables. 对于一对多,您的一个表上只有一个外键。 This is probably your case: one person can author many articles, and the relationship is stored in the field blog_posts.person_id. 这可能是您的情况:一个人可以撰写许多文章,并且该关系存储在blog_posts.person_id字段中。 In this case, the query above is exactly the same. 在这种情况下,上面的查询是完全相同的。 The only difference is you can't go the other way 唯一的区别是你不能走另一条路

For a many-to-many relationship, you would have to have a join table in the middle between the two tables (some people refer to these as 'pivot' tables). 对于多对多关系,您必须在两个表之间的中间有一个连接表(有些人称这些表为“枢轴”表)。 This table would only need to record both the people_ids and the blog_post_ids, and represents a situation where you would allow for many people to author a single blog post, and a single person to also be an author for many blog posts. 该表仅需要记录people_ids和blog_post_ids,并代表一种情况,您将允许许多人撰写单个博客文章,而一个人同时也是许多博客文章的作者。 The query for this relationship would be different: 此关系的查询将有所不同:

SELECT * FROM blog_posts b
    INNER JOIN blog_post_people bp ON bp.blog_post_id = b.blog_post_id
    INNER JOIN people p ON p.people_id = bp.people_id

Again, using the * here when selecting will give you the columns from all three tables, and you should probably avoid using it. 同样,在选择时在此处使用*将为您提供所有三个表中的列,您应该避免使用它。

您可以从多个表格中进行选择

$sql_query="SELECT * FROM blog_posts,people WHERE people.id=$id"; ORDER BY id DESC 

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

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