Get all records in first tbl and matching in second tbl (indicating ones that existed)

前端 未结 1 1825
情歌与酒
情歌与酒 2021-01-28 07:49

Looking to get ALL UserName from UsersDataTbl and matching UserName from EnrollmentsTbl if it exist. but I want to identify w

相关标签:
1条回答
  • 2021-01-28 08:16

    You can us the is not null to test if a field's value is null or not and return a true or false. If the username does not exist in EnrollmentsTbl then the left join will return null in its place.

    I moved the e.ClassName LIKE 'Word% criterion from the where clause to the join condition, since it changed the left join into an inner join defeating the whole purpose of the query.

    SELECT u.UserName
         , (e.UserName IS NOT NULL) as user_exists_in_EnrollmentsTbl 
      FROM UsersDataTbl u
      LEFT 
      JOIN EnrollmentsTbl e
        ON u.UserName = e.UserName 
            AND e.ClassName LIKE 'Word%'
     WHERE u.UserName LIKE 'bar%' 
    
    0 讨论(0)
提交回复
热议问题