MySQL基于ENUM值进行选择

时间:2013-06-13 12:57:20

标签: mysql sql select join enums

我在MySQL数据库中有一个供应商目录表,该表具有关联的供应商映射表。此表针对单个供应商映射以下标准:

  1. 服务。
  2. 专长
  3. 标准
  4. 仅为背景信息,每个都有值存储在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子句有关,但无法弄清楚是什么。

    如果有人有任何想法,我会很感激吗?

2 个答案:

答案 0 :(得分:3)

如果您查看查询的条件:

(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';

答案 1 :(得分:1)

看看你的布尔逻辑。最后你有一个关联函数(“AND”)

所以这个

(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'); 

仅对具有BOTH

的内容返回true
supplier_mappings.entity_type = 'SPECIALITY'

supplier_mappings.entity_type = 'KEY_SERVICE' 

这些东西无法同时出现。 (这也适用于第三个值,顺便说一下id)。你不希望所有这些东西一下子。 一个例子是一个简单的案例:如果你想从一个简单的表中得到id=3的行和id=4的行,你的逻辑将不是WHERE id=3 AND id=4,因为有没有可以兼得的行。相反,你要求WHERE id=3 OR id=4

相反(我不确定这是完全修复所有内容,但它应该让你去),你想为这些单独的案例做一个OR:

(supplier_mappings.entity_type = 'KEY_SERVICE' AND supplier_mappings.entity_id =    '55')
OR (supplier_mappings.entity_type = 'SPECIALITY' AND supplier_mappings.entity_id = '218')
OR (supplier_mappings.entity_type = 'STANDARD' AND supplier_mappings.entity_id = '15');