2

I have a table of data, that looks something like this:

ID       CODE

1        FOO
1        FOO2
1        FOO3
1        BADCODE
2        FOO
2        FOO2

When I perform a query on this table I essentially want to discount all rows that contain the same ID if a bad code is found. So in the example above nothing with an ID of 1 would be returned as a badcode has been found in one of the rows.

Sorry if this hasn't been explained in the most eloquent way. Any ideas?

2 Answers 2

3

SELECT * FROM mytable WHERE id NOT IN (SELECT id FROM mytable WHERE code = 'BADCODE')

3
SELECT  *
FROM    mytable mo
WHERE   NOT EXISTS
        (
        SELECT  NULL
        FROM    mytable mi
        WHERE   mi.id = mo.id
                AND mi.code = 'BADCODE'
        )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.