I have a quick question, if someone can help, I would be grateful
I have the following tables
User, Video, User_video
user has userdetails (user_id, name, email)
video has videoDetails (video_id, video_url, video_price)
user_video is the mapping table which has (user_id,video_id,price,dttm)
currently, if I do a select from video table, it gives me all available videos which is fine. The current return format is as follows:
123 | Video 1 | desc of video1 | /abvu274vasag.3gp | 5.00 124 | video 2 | desc of video2 | /dh63ddh3dag.3gp | 6.00
So far, so good, but now, I need to get the details of the logged in user, as in which videos he has purchased and which ones he has not, so in the above response, next to each video, I need a "Y" or "N" flag (by referencing the user_video) table.
I need the output as follows:
123 | Video 1 | desc of video1 | /abvu274vasag.3gp | 5.00 | Y 124 | video 2 | desc of video2 | /dh63ddh3dag.3gp | 6.00 | N
It is a PostgreSQL database. And I am not an expert on databases, I am familiar with only the basics.
If someone can guide me in this regards, it will be most helpful.
Best Answer
Assuming
(video_id, user_id)
is unique inuser_video
, a plainLEFT JOIN
would do the job:$current_user_id
being the ID of the current user.If
(video_id, user_id)
is not unique, you could addGROUP BY
count()
doesn't count NULL values.Or use an
EXISTS
semi-join to avoid duplicates:Or use the PostgreSQL-specific
DISTINCT ON
, that's particularly handy if you need the result in a certain sort order anyway.