I have a a suppliers directory table in a MySQL database which has an associated suppliers mappings table. This table maps the following criteria against an individual supplier:
- Services.
- Specialities
- Standards
Just for background info each of these have values stored in 3 individual corresponding tables. All services that can be provided by any supplier are listed in the services table and the the mapping table captures the services which are offered by an individual supplier. A supplier can provide more than one service so they would have a mapping for each service they provide. Same goes for specialities and standards.
Where I am running into difficulties is on a search query where a user can query a list of suppliers on any one or all three of the above criteria. So for example they can search for a supplier with service a, speciality b and standard c. They can't search for multiple values on a service, speciality or standard
My mappings table looks like the following:
id int(11) unsigned NOT NULL
supplier_id int(11) unsigned NOT NULL
entity_type enum('KEY_SERVICE','STANDARD','SPECIALITY') NOT NULL
entity_id int(11) NOT NULL
Where entity_type maps is used to indicate the entity type being mapped and entity_id indicates the individual entity.
My query is as follows:
SELECT DISTINCT supplier_mappings.supplier_id, suppliers.company_name
FROM supplier_mappings
JOIN suppliers ON suppliers.id = supplier_mappings.supplier_id
WHERE (supplier_mappings.entity_type = 'KEY_SERVICE' AND supplier_mappings.entity_id = '55')
AND (supplier_mappings.entity_type = 'SPECIALITY' AND supplier_mappings.entity_id = '218')
AND (supplier_mappings.entity_type = 'STANDARD' AND supplier_mappings.entity_id = '15');
which should return all suppliers who have a key service with the id 55, a speciality with the id 218 and a standard with the id 15. However it just returns an empty result set even though I know there is at least one supplier with these defined. It appears to be something to do with the compounded AND clauses but can't figure out what.
Would appreciate it if anyone has any ideas?