I have table in a PostgreSQL 9.2.24 database:
CREATE TABLE episodes_list (
mid serial PRIMARY KEY -- movie id
, title text NOT NULL
, is_movie integer NOT NULL -- 1 for movie, 0 for episode
);
I need to display on the left titles of episodes, on the right titles of all movies (even those which don't have episodes).
103 | Microcosmos (high quality) | 1
103 | Snails' sexual act | 0
103 | Landing | 0
Here are a few records. So eg. 'Microcosmos' should be on the right twice, with snail's sex act and landing on the left. That's how it should look like:
Landing | Microcosmos (high quality)
Snails' sexual act | Microcosmos (high quality)
I wrote something like this:
select e1.title, e2.title from episodes_list e1 right join episodes_list e2 on e1.is_movie = 0 and e2.is_movie = 1 and e1.mid = e2.mid;
But it still shows on the right titles of episodes, rather then only movie titles (so I'm getting additional lines which I shouldn't be getting). So it looks like this: But it looks like this:
Landing | Microcosmos (high quality)
Snails' sexual act | Microcosmos (high quality)
| Snails' sexual act
What am I doing wrong here and how the query should look like?