MySQL snippet - Selecting from table a items that aren't in table b.

Today I needed to select something from a table on the condition that the ID of the row in the table did not appear in another table.

Initially I thought this was going to be slightly difficult to do, or at least a bit convoluted but it turned out to be really simple.

SELECT table_a.id FROM table_a LEFT JOIN table_b ON table_a.id = table_b.aid WHERE table_b.aid IS NULL;

Since you are using a LEFT JOIN table_b.aid can return NULL values, and these are the ones you are looking for.

Recent posts View all

Talk CSS

FrontEnders - Web Performance

Sharing some of my thoughts on a FrontEnders meetup on the topic of Web Performance

Conferences

Tips for how to ask for time off to attend conferences

Asking for time off to attend a conference can be nerve-racking, these tips will get you closer to attending the conferences you're interested in.