MySQL Join on NOT Condition


/ Published in: MySQL
Save to your folder(s)

This was a strange case where I had to select all the items that were NOT assigned to a user. The data structure assumes that there is a 1 to 1 relationship between a user and an item. This is a simplified version of the query, but it's one of those queries that takes a while to figure out.


Copy this code and paste it in your HTML
  1. SELECT item.id, item.name FROM items LEFT OUTER JOIN users ON users.item_id = items.id where users.item_id is null

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.