Skip to content
Advertisement

MySQL: Best way finding rows, where associated list match a given list

I built a SQL Statement, explained here:

I have a table Account like this:

I have a table Customer like this:

I have a relation Customer_To_Account like this:

I want to find all accounts which are assigned to customers in a given list like (111111, 222222). The returned accounts MUST contain at least one of the given customers and they MUST NOT contain a customer that is NOT in the given list.

Expected Result:

  • For list (111111) the result must be only the account 3, as the account 3 cointains the customer 111111 and NOT contains any other customer. The account 1 and 2 e.g. contain the costumers 222222 which is not in the list (111111) -> so not in the result.

  • For List (111111, 222222) the result must be account 2 and 3, as they contain at least one of the given customers (111111, 222222) and the NOT contain any other customer. The Account 1 contains the customer 333333, which is not in the list (111111, 222222) -> so not in the result.

My query:

This returns the correct result, but hasn’t the query a very bad performance, as it selects all IDs of accounts, that have at least one customer, that is not in the given list.

Does anybody has a better idea/query for such a use case?

Thanks a lot!

heiwil

Advertisement

Answer

How about using aggregation?

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