Mysql left join with condition in right table

前端 未结 2 1134
说谎
说谎 2020-12-07 01:42

I have been trying to solve this issue for a while, hope anyone help me. I am having two table, the first table is

Table Name : OnlineTest

相关标签:
2条回答
  • 2020-12-07 01:44

    Place the d.userid = 1 predicate in the ON clause:

    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' 
    

    This will return all rows from onlinetest, having columns of userstatus filled with nulls where predicate d.userid = 1 fails.

    0 讨论(0)
  • 2020-12-07 02:01

    You can also use left outer Join as below :

    SELECT        c.OnlineTestId, d.userId, d.status
    FROM            OnlineTest AS c LEFT OUTER JOIN
                             UserStatus AS d ON d.onlineTestId = c.OnlineTestId AND d.userId = 1
    WHERE        (c.category = 'English')
    
    0 讨论(0)
提交回复
热议问题