JOIN two tables to show already purchased items

Posted by Norbert on Stack Overflow See other posts from Stack Overflow or by Norbert
Published on 2012-09-03T09:36:04Z Indexed on 2012/09/03 9:37 UTC
Read the original article Hit count: 209

Filed under:
|
|

I have a table where I keep all my templates:

templates

  • template_id
  • template_name
  • template_price

These templates can be purchased by a registered user and then are inserted in the payments table:

payments

  • payment_id
  • template_id
  • user_id

Is there a way to join these two tables and get not just a list of templates that have been purchased by a certain user, but all the templates? And then figure out from there which ones have already been purchased?

I used this SELECT, but only the ones that the user bought showed up. I would like to have all the rows from templates, but empty in case the user_id doesn't match.

SELECT *
FROM templates
LEFT JOIN payments
ON templates.template_id = payments.template_id
WHERE user_id = 2
GROUP BY templates.template_id

© Stack Overflow or respective owner

Related posts about mysql

Related posts about query