A
Andreas Boehmer
I am having this sql statement (see below) that drives me mad. Can't figure
out why it is returning too many results (multiple of the same record).
I am searching for a keyword (let's say "Chips") in the name of a company,
as well as in the name of a catgory that the company is related to. I know
the problem lies in my many:many relationship with the table
companyCategory. Let's assume the category "Chips" that I am searching for
is assigned three times to the same company. In that case, my sql search
will return the same company three times. But I only want it once!
Perhaps somebody can give me a tip of how to proceed:
SELECT companies.name FROM companies, categories, companyCategory WHERE
companies.companyID = companyCategory.companyID AND categories.categoryID =
companyCategory.categoryID AND (companies.name ='Chips' OR
companies.companyID IN (SELECT DISTINCT companies.companyID FROM companies,
categories, companyCategory WHERE companies.companyID =
companyCategory.companyID AND categories.categoryID =
companyCategory.categoryID AND categories.name ='Chips'))
These are the tables and their main columns I have got:
companies (companyID, name)
companyCategory (companyID, categoryID)
categories (categoryID, name)
Thanks heaps for your help!
out why it is returning too many results (multiple of the same record).
I am searching for a keyword (let's say "Chips") in the name of a company,
as well as in the name of a catgory that the company is related to. I know
the problem lies in my many:many relationship with the table
companyCategory. Let's assume the category "Chips" that I am searching for
is assigned three times to the same company. In that case, my sql search
will return the same company three times. But I only want it once!
Perhaps somebody can give me a tip of how to proceed:
SELECT companies.name FROM companies, categories, companyCategory WHERE
companies.companyID = companyCategory.companyID AND categories.categoryID =
companyCategory.categoryID AND (companies.name ='Chips' OR
companies.companyID IN (SELECT DISTINCT companies.companyID FROM companies,
categories, companyCategory WHERE companies.companyID =
companyCategory.companyID AND categories.categoryID =
companyCategory.categoryID AND categories.name ='Chips'))
These are the tables and their main columns I have got:
companies (companyID, name)
companyCategory (companyID, categoryID)
categories (categoryID, name)
Thanks heaps for your help!