Replacement / Alternative for UNION with a single JOIN Select Statement
I have a table called posts
and I want retrieve
everything records
that meets the conditions below in select statement
and returns 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, using the operator UNION
as shown below, I can return one 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
Tried below JOIN
query but didn't get 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 get records using JOIN
OR using one SELECT
, not UNION
to get the desired result as shown above.
source to share