gpt4 book ai didi

Mysql 左连接右表中的条件

转载 作者:行者123 更新时间:2023-11-29 15:44:40 25 4
gpt4 key购买 nike

我一直在尝试解决这个问题,希望有人帮助我。我有两张 table ,第一张 table 是

表名称:OnlineTest

OnlineTestId     category    subcategory                                                                   
1 English Spelling
2 English Grammar
3 English Antonyms
4 English Synonyms

第二个表是

表名称: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

结果

OnlineTestId    userId        status
1 1 Finished
2 1 Not Finished
3 null null
4 null null

我已经尝试过这个查询,

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;

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

如何带来上述结果?

最佳答案

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'

这将返回来自 onlinetest 的所有行,其中 userstatus 列填充有 null,其中谓词 d.userid = 1 失败。

关于Mysql 左连接右表中的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57199458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com