Can SQL be used to find all the brands that has the most common categories?
For example, the brand "Dove" can have category of Soap, Skin Care, Shampoo It is to find all the brands that has the most matching categories, in other words, the most similar brands.
It can be done programmatically using Ruby or PHP: just take a brand, and loop through all the other brands, and see how many matching categories there are, and sort by it. But if there are 2000 brands, then there needs to be 2000 queries per brand. (unless we pre-cache all the 2000 query results, so for all 2000 brands, we re-use those results)
Can it be done by SQL / MySQL by 1 query?
Say, the table has:
entities
--------
id
type = brand or category or product
name
entities_parent_child
--------------------
parent_id
child_id
the table above has an entry for each parent = brand and child = product, and also an entry for each parent = category and child = product, so brand has to relate to category by products.
I think the hard part for SQL is: find all the maximum matching counts, and sort by those numbers.