The presence of these tables:
customers
---------------------
`id` smallint(5) unsigned NOT NULL auto_increment,
`name` varchar(100) collate utf8_unicode_ci default NOT NULL,
....
customers_subaccounts
-------------------------
`companies_id` mediumint(8) unsigned NOT NULL,
`customers_id` mediumint(8) unsigned NOT NULL,
`subaccount` int(10) unsigned NOT NULL
I need to get all customers who have been assigned more than one subaccount for the same company.
This is what I have:
SELECT * FROM customers
WHERE id IN
(SELECT customers_id
FROM customers_subaccounts
GROUP BY customers_id, companies_id
HAVING COUNT(subaccount) > 1)
This request is too slow, though. This is even slower if I add the DISTINCT modifier to customer_id in the SELECT of the subquery, which at the end gets the same list of clients for the entire query. Perhaps there is a better way without a subquery, something will help faster, and I'm not sure if it will find the correct exact list.
Any help?
source
share