joanb
joanb

Reputation: 349

MYSQL Query Help, Not Exists but also exists with a condition

I am not sure if this is even possible but thought i'd put it out there. Is it possible to select records from one table that do not exists in another table but also records that may exists adding a condition? Here is the query right now.

SELECT first_name,last_name 
FROM clients 
WHERE clients.status = 'Active' 
    AND (NOT EXISTS(
        SELECT * 
        FROM assignments AS c 
        WHERE c.client_id = clients.client_id )) 

this will give me the records that dont exists, is it possible to add records that exists with a condition? i.e I need to also see the records where assignments.active = 0

Upvotes: 0

Views: 212

Answers (1)

Barmar
Barmar

Reputation: 780723

Add the active condition to the subquery.

SELECT first_name,last_name 
FROM clients 
WHERE clients.status = 'Active' 
    AND (NOT EXISTS (
        SELECT * 
        FROM assignments AS a
        WHERE a.client_id = clients.client_id 
            AND a.active = 1
    ))

Upvotes: 1

Related Questions