Substitute/Alternative for UNION with single JOIN Select Statement

Viewed 444

I've a table called posts and i want to retrieve all the records which satisfies the below conditions in select statement and return a single table -

select ID from posts where UserID= 23487 and postlevel <> 1
select ID from posts where ParentID in (select ID from posts where UserID= 23487 and postlevel <> 1)

Now, by using UNION operator like below i can return a single table -

select ID from posts where UserID= 23487 and postlevel <> 1
UNION 
select ID from posts where ParentID in (select ID from posts where UserID= 23487 and postlevel <> 1)

Output

enter image description here

Tried below JOIN query but did not get the expected result and returns NULL-

select ID from posts cs
LEFT JOIN posts cs1 ON cs.ID = cs1.ID
where cs.UserID = 23487 and cs.PostLevel <>1 and cs.ParentID = cs1.ID

Expected

I want to fetch the records using JOIN OR using a single SELECT and not UNION to get the desired output as shown above.

3 Answers
Related