Skip to content
Advertisement

Data from the table based on the received

Table Users: id, login, name, age

Table views: id, date, user_id, post_id

Table other: id, timeD (date), post_id

What is already there:

SELECT date, user_id
    FROM public.views
    Where
    date IN (Select max(date) from public.views where post_id = 2
            group by user_id)

Result: date and user_id

I need to add in request to display data about the user (login, name, age), the user id is optional and the timeD column

Example: date timeD login name age

Note: the other table may be empty and then i need to output timeD as null, for example

Advertisement

Answer

Try to use left join:

SELECT date, 
       timeD, 
       login, 
       name,
       age
FROM public.views
LEFT JOIN Users 
     ON Users.id = views.user_id
LEFT JOIN other
      ON other.post_id = views.post_id
Where date IN (Select max(date) from public.views where post_id = 2
            group by user_id)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement