0

I want to join two tables using a join

SELECT * FROM posts
LEFT JOIN voted ON posts.post_id = voted.id

Which produces this:

enter image description here

How would I create query using:

ORDER BY date_posted DESC FETCH FIRST 5 ROW ONLY 

on the Posts Table to return this result

enter image description here

Edit 1: duplicate post_id

How would I make it so that the uuid on the user_id column is only 82411850-

enter image description here

Edit 2: Final query thanks to Mr.Linoff

SELECT p.post_id, p.date_posted, p.posted_by,
   v.user_id, v.votes
FROM posts p LEFT JOIN
 voted v
 ON p.post_id = v.id
 AND v.user_id = '82411580...'

ORDER BY p.date_posted DESC
FETCH FIRST 5 ROW ONLY ;

1 Answer 1

1

You have a collision of ids. Be explicit about the columns you are selecting.

Then I think you have basically the right logic:

SELECT p.post_id, p.date_posted, p.posted_by,
       v.user_id, v.votes
FROM posts p LEFT JOIN
     voted v
     ON p.post_id = v.id
ORDER BY p.date_posted DESC
FETCH FIRST 5 ROW ONLY ;
2
  • Thanks for the assistance but I'm still getting a duplicate on post_id. Unsure why (added a pic above)
    – DIRTY DAVE
    Jun 10, 2019 at 23:30
  • Finally got it, need to add v.user_id = "82411850.." after the ON
    – DIRTY DAVE
    Jun 11, 2019 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.