我在MySQL数据库中有一个供应商目录表,它有一个关联的供应商映射表.此表针对单个供应商映射以下标准:
>服务.
>特色菜
>标准
仅用于背景信息,每个都具有存储在3个单独对应表中的值.可以由任何供应商提供的所有服务都列在服务表中,映射表捕获由单个供应商提供的服务.供应商可以提供多种服务,因此他们可以为他们提供的每项服务制作映射.同样适用于专业和标准.
我遇到困难的地方是搜索查询,用户可以在上述任何一个或全部三个标准中查询供应商列表.例如,他们可以搜索具有服务a,专业b和标准c的供应商.他们无法在服务,专业或标准上搜索多个值
我的映射表如下所示:
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
其中entity_type映射用于指示要映射的实体类型,entity_id用于指示单个实体.
我的查询如下:
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');
这应该返回所有具有id为55的密钥服务的供应商,id为218的专业和id为15的标准.但是它只返回一个空的结果集,即使我知道至少有一个供应商具有这些定义.它似乎与复合的AND子句有关,但无法弄清楚是什么.
如果有人有任何想法,会很感激吗?
(supplier_mappings.entity_type =’KEY_SERVICE’和supplier_mappings.entity_id = ’55’)
AND(supplier_mappings.entity_type =’SPECIALTY’和supplier_mappings.entity_id =’218′)
AND(supplier_mappings.entity_type =’STANDARD’和supplier_mappings.entity_id = ’15’)
例如:
supplier_mappings.entity_type =’KEY_SERVICE’和supplier_mappings.entity_type =’SPECIALTY’
将返回FALSE值.
您必须使用单独的表foreach条件,如下所示
SELECT DISTINCT suppliers.ID,suppliers.company_name
FROM suppliers,supplier_mappings mapA,supplier_mappings mapB,supplier_mappings mapC
WHERE suppliers.ID = mapA.supplier_id
AND suppliers.ID = mapB.supplier_id
AND suppliers.ID = mapC.supplier_id
AND mapA.entity_type = 'KEY_SERVICE' AND mapA.entity_id = '55'
AND mapB.entity_type = 'SPECIALITY' AND mapB.entity_id = '218'
AND mapC.entity_type = 'STANDARD' AND mapC.entity_id = '15';