I want to get data if orgid = 2 or if there is no row at all for the uid. orgid is an integer. The closest thing I could think of is to do IS NULL but I’m not getting data for the uid that doesn’t have an orgid row. Any idea?
select u.uid,u.fname,u.lname from u
inner join u_org on u.uid = u_org.uid
inner join login on u.uid = login.uid
where u_org.orgid=2 or u_org.orgid is NULL
and login.access != 4;
Basically the OR is if u_org.orgid row doesn’t exist.
If there is “no row at all for the uid”, and you
JOINlike you do, you get no row as result. UseLEFT [OUTER] JOINinstead:Also, you need the parenthesis I added because of operator precedence. (
ANDbinds beforeOR).I use
IS DISTINCT FROMinstead of!=in the last WHERE condition because, again,login.accessmight beNULL, which would not qualify.However, since you only seem to be interested in columns from table
uto begin with, this alternative query would be more elegant:This alternative has the additional advantage, that you always get one row from
u, even if there are multiple rows inu_orgorlogin.