Mysql左边的连接条件在右表中

Sub*_*h L 6 mysql left-join where

我一直试图解决这个问题,希望有人帮助我.我有两张桌子,第一张桌子是

表名称:OnlineTest

OnlineTestId     category    subcategory                                                                   
     1            English      Spelling                                                                    
     2            English      Grammar
     3            English      Antonyms
     4            English      Synonyms
Run Code Online (Sandbox Code Playgroud)

第二个表是

表名:UserStatus

Id     userId    status         onlineTestId
1       1        Finished           1
2       1        Not Finished       2
3       2        Not Finished       1
4       2        Finished           3
5       3        Not Finished       4
Run Code Online (Sandbox Code Playgroud)

结果

OnlineTestId    userId        status
    1               1         Finished
    2               1         Not Finished
    3               null      null
    4               null      null
Run Code Online (Sandbox Code Playgroud)

我试过这个查询,

select c.onlinetestid, d.userid, d.status from onlinetest c left join userstatus d on d.onlinetestid = c.onlinetestid
where c.category = 'English' and d.userid = 1;
Run Code Online (Sandbox Code Playgroud)

但是这个查询带来了结果的前两行,而不是最后两行,其中userId和status为null.

如何带来上述结果?

Gio*_*sos 13

d.userid = 1谓词放在ON子句中:

select c.onlinetestid, d.userid, d.status 
from onlinetest c 
left join userstatus d on d.onlinetestid = c.onlinetestid and d.userid = 1
where c.category = 'English' 
Run Code Online (Sandbox Code Playgroud)

这将返回所有行onlinetest,其中包含userstatus填充nulls的列,其中谓词d.userid = 1失败.