Skip to content
Advertisement

How to Use a Subquery in MySQL in the Where clause that is an argument to an OR operator?

I am trying to filter a table for specific markets by using a subquery. I want to include all null values and exclude the markets that are included in the subquery.

This query isn’t filtering out the markets I want to get rid of.

And this created a SQL compilation error.

When I list the markets I want to exclude individually, the query works but I am trying to make it more dynamic.

Advertisement

Answer

I would just use not exists:

Note that this will allow records where market is null (since, in this case, c.id = l.market will never be fullfilled).

Another option is to use the left join antipattern:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement