Mysql MSQL,检索所有项目,即使它没有任何匹配的外部行

Mysql MSQL,检索所有项目,即使它没有任何匹配的外部行,mysql,sql,Mysql,Sql,标题表 | titleId | title | 1 test5 2 test3 订阅表 | userId | titleId | 1 1 在本例中,用户1订阅了标题test5 我想要的是: 返回所有标题,如果它们具有指定用户的匹配行,则显示该标题。例如,对于用户1: title | subscribed test5 1 test3 - 我想到了这个: SELECT title.

标题表

   | titleId | title |
       1       test5 
       2       test3
订阅表

   | userId | titleId |
       1       1 
在本例中,用户1订阅了标题test5

我想要的是: 返回所有标题,如果它们具有指定用户的匹配行,则显示该标题。例如,对于用户1:

title | subscribed
test5      1
test3      -
我想到了这个:

SELECT title.id, title.title, subscribe.userId  FROM title   JOIN subscribe  ON title.id  = subscribe.uesrId 
where  subscribe.userId = 1;

我还想在下面一行

test3      -

我想你只需要一个
左连接

SELECT t.id, t.title, s.userId
FROM title t LEFT JOIN
     subscribe s
     ON t.id  = s.titleId AND s.userId = 1;
请注意,
WHERE
条件现在必须进入
ON
子句

此外,表别名的使用使查询更易于编写和读取

JOIN
条件可能应该是
titleid
而不是
userid


是用于上述查询的SQL FIDLE。

谢谢,但在本例中,“test5 1”返回两次。我通过添加“group by title.id”修复了该问题,并将其修复。谢谢,请用“分组方式”更新您的答案。
SELECT t.id, t.title, s.userId
FROM title t LEFT JOIN
     subscribe s
     ON t.id  = s.titleId AND s.userId = 1;