简体   繁体   English

如何从已知和未知作者的帖子表中获取帖子

[英]How to get posts from posts table with both known and unknown authors

I have a posts table in sql which its author column is connected to users table. 我在sql中有一个posts表,其作者列连接到users表。 I use a single sql query to get post from posts table and the author's name from users table. 我使用单个SQL查询从posts表中获取帖子,并从users表中获取作者的名称。 My posts table looks like this: 我的帖子表看起来像这样:

+----+-----------------+-----------------+--------+
| id | title           | label           | author |
+----+-----------------+-----------------+--------+
| 22 | Post 1          | post-1          |      2 |
| 24 | Post 2          | post-2          |      4 |
| 25 | Post 3          | post-3          |      4 |
| 26 | Post 4          | post-4          |      5 |
| 27 | Post 5          | post-5          |      6 |
| 28 | Post 6          | post-6          |      2 |
| 29 | Post 7          | post-7          |      2 |
| 30 | Post 8          | post-8          |      2 |
| 32 | Post 9          | post-9          |      2 |
+----+-----------------+-----------------+--------+

I use this sql query to get post title, label and its authors name and surname. 我使用这个SQL查询来获取帖子标题,标签及其作者姓名和姓氏。

SELECT `posts`.id, `posts`.title, `posts`.label, users.id AS author, users.name AS name, users.surname AS surname
FROM `posts`
INNER JOIN users
ON users.id = posts.author
ORDER BY `date` DESC;

This works perfectly fine but it only returns posts with known authors which is id 2 because in users table I only have author id 2. Other authors (4, 5 and 6) are missing. 这完全正常,但它只返回已知作者的帖子,这是id 2,因为在用户表中我只有作者ID 2.其他作者(4,5和6)丢失。 So instead of not showing the posts with unknown authors, I want to show them null as its name and surname variable. 因此,我没有显示具有未知作者的帖子,而是希望将它们显示为null作为其名称和姓氏变量。 By the way the result is; 顺便说一句,结果是;

+----+----------------+----------------+--------+------+---------+
| id | title          | label          | author | name | surname |
+----+----------------+----------------+--------+------+---------+
| 32 | Post 9         | post-9         |      2 | Jack | Smith   |
| 30 | Post 8         | post-8         |      2 | Jack | Smith   |
| 29 | Post 7         | post-7         |      2 | Jack | Smith   |
| 28 | Post 6         | post-6         |      2 | Jack | Smith   |
| 22 | Post 1         | post-1         |      2 | Jack | Smith   |
+----+----------------+----------------+--------+------+---------+

Instead of INNER JOIN you have to use LEFT JOIN. 而不是INNER JOIN,你必须使用LEFT JOIN。 The result of an inner join just shows entities which are represented in both tables. 内部联接的结果只显示在两个表中表示的实体。 A left join adds a null value, when an entity is represented in the first table but not in the second. 当实体在第一个表中表示而在第二个表中不表示时,左连接会添加空值。

Try to use left Join : 尝试使用左连接:

one more this create the foreign key constrain while refer another table value. 还有一个在创建外键约束时引用另一个表值。

SELECT `posts`.id, `posts`.title, `posts`.label, posts.id AS author, 
  users.name AS name, users.surname AS surname
    FROM `posts`
       LEFT JOIN users
          ON users.id = posts.author
             ORDER BY `date` DESC;

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

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