How to select rows that don't have value in a second table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

How to select rows that don't have value in a second table

Post by Mr Tech »

Basically I have a main table (accounts) and a meta table (accounts_meta)... The meta table looks like this:

Code: Select all

id | account_id | meta_key | meta_value
What I want to do is only select accounts that do not have 'referrer_paid' as a row in the accounts_meta table...

Here is my code so far...

Code: Select all

SELECT a.* FROM accounts AS a 
    LEFT JOIN accounts_meta AS am ON a.id = am.account_id AND am.meta_key != 'referrer_paid' 
    WHERE a.account_referrer != ''
    GROUP BY a.id
Hopefully I am making sense. What am I doing wrong?
Post Reply