MySQL Join Same Table

mysql, sql

Solution

To achieve this you should use the LEFT OUTER JOIN operation joining the same table.

SELECT a.*
FROM meta_data a
LEFT OUTER JOIN meta_data b ON a.post_id = b.post_id AND b.meta_value = 'def'
WHERE 
a.meta_value = 'abc'
AND b.post_id IS null

Problem

I have the table 'meta_data' with the following fields: - id - post_id - meta_key - meta_value I'd like to loop through and display a list of EACH post (`post_id`) that has an entry for `meta_key='abc'` but not one for `meta_key='def'` Basically, every post that has a `meta_key='abc'` entry should have a `meta_key='def'` entry. I want to generate the list so I can add the missing `meta_key='def'` entries.

Original source