或者运算符返回更多行然后预期的mysql

时间:2016-07-01 11:49:45

标签: mysql

我有一张桌子上有一些与汽车模型有关的物品。这些项目可以有几个类别。我必须为sc_products表中存储的每个项目选择与汽车相关的所有类别以及所有给定类别类别。

这是我的问题:

SELECT 
t15_catalogue_line.
T15_GROUP, 
sc_products.product_code,
sc_products.unic, sc_products.name_ru, UPPER(TRIM(sc_products.brief_description_ru)) AS brief_description_ru, sc_products.suupplier, price.Price, sc_group_discounts.`action`, sc_group_discounts.procent, sc_products.productID, price.in_stock, price.supplier, t10_item.T10_ITEM, t10_item.unic, t10_item.T10_DESC, t10_item.T10_IMG, t10_item.T10_ITEM_GROUP, t10_item.T10_FIELD1, t10_item.T10_FIELD2, t10_item.T10_FIELD3, t10_item.T10_FIELD4, t10_item.T10_FIELD5, t10_item.T10_FIELD6, t10_item.T10_FIELD7, t10_item.T10_FIELD8, t10_item.T10_FIELD9, t10_item.T10_FIELD10, t10_item.T10_FIELD11, t14_item_fields.T14_ITEM_GROUP, t14_item_fields.T14_FIELD, t14_item_fields.T14_NAME, t14_item_fields.T14_UNIT, t14_item_fields.T14_SEARCH 
FROM 
sc_products 
LEFT OUTER JOIN t15_catalogue_line ON (sc_products.unic = t15_catalogue_line.unic) 
LEFT OUTER JOIN price ON (sc_products.unic = price.unic) AND (sc_products.suupplier = price.postavchik) 
LEFT OUTER JOIN sc_group_discounts ON (sc_products.item_group = sc_group_discounts.item_group) 
LEFT OUTER JOIN t10_item ON (sc_products.unic = t10_item.unic and sc_products.CatText=t10_item.CatText and sc_products.brief_description_ru=t10_item.brand) 
LEFT OUTER JOIN t14_item_fields ON (t10_item.T10_ITEM_GROUP = t14_item_fields.T14_ITEM_GROUP) 
WHERE 
sc_products.CatText = 'bracke mechanism' or 
sc_products.CatText='bracke montage ' or 
sc_products.CatText='hydraulic repair ' AND   
t15_catalogue_line.T15_CARTYPE = '30442' AND 
t15_catalogue_line.T15_GROUP = '666' and 
sc_products.unic is not null and 
sc_products. unic!='' 
GROUP BY sc_products.product_code,sc_products.brief_description_ru, sc_products.suupplier 
ORDER BY ISNULL( price.price),price.price ASC

查询会返回这些caegories中的所有项目,但它们与自动无关,因此表格t15_catalog行不会参与查询可能是什么解决方案

3 个答案:

答案 0 :(得分:1)

这个逻辑:

a OR b OR c AND d相当于a OR b OR (c AND d)而不是(a OR b OR c) AND d,这似乎是您想要的。

您需要在OR块周围添加括号,如下所示:

WHERE 
(
    sc_products.CatText = 'bracke mechanism'
 OR sc_products.CatText = 'bracke montage ' 
 OR sc_products.CatText = 'hydraulic repair ' 
)
AND t15_catalogue_line.T15_CARTYPE = '30442' 
AND t15_catalogue_line.T15_GROUP = '666' 
AND sc_products.unic is not null 
AND sc_products. unic!='' 

答案 1 :(得分:1)

我非常怀疑多个()周围缺少的括号AND是您的问题。

  

查询返回那些caegories中的所有项目,它们与auto无关,因此t15_catalog行不参与查询可能是什么解决方案

您的分组逻辑现在完全按照您的描述进行,下面是我认为您打算做的事情:

WHERE (sc_products.CatText = 'bracke mechanism' or sc_products.CatText='bracke montage ' or sc_products.CatText='hydraulic repair ') AND t15_catalogue_line.T15_CARTYPE = '30442' AND t15_catalogue_line.T15_GROUP = '666' and sc_products.unic is not null and sc_products. unic!='' GROUP BY sc_products.product_code,sc_products.brief_description_ru, sc_products.suupplier ORDER BY ISNULL( price.price),price.price ASC

答案 2 :(得分:0)

您可能希望在查询中使用paranthesis来获得所需的结果。 <怎么样

SELECT 
    t15_catalogue_line.
    T15_GROUP, 
    sc_products.product_code,
    sc_products.unic, sc_products.name_ru, 
    UPPER(TRIM (sc_products.brief_description_ru)) AS brief_description_ru,
    sc_products.suupplier, price.Price, sc_group_discounts.`action`,
    sc_group_discounts.procent, sc_products.productID,
    price.in_stock, price.supplier, t10_item.T10_ITEM, t10_item.unic,
    t10_item.T10_DESC, t10_item.T10_IMG, t10_item.T10_ITEM_GROUP,
    t10_item.T10_FIELD1, t10_item.T10_FIELD2, t10_item.T10_FIELD3,
    t10_item.T10_FIELD4, t10_item.T10_FIELD5, t10_item.T10_FIELD6,
    t10_item.T10_FIELD7, t10_item.T10_FIELD8, t10_item.T10_FIELD9, 
    t10_item.T10_FIELD10, t10_item.T10_FIELD11,
    t14_item_fields.T14_ITEM_GROUP, t14_item_fields.T14_FIELD,
    t14_item_fields.T14_NAME, t14_item_fields.T14_UNIT,
    t14_item_fields.T14_SEARCH 
FROM 
    sc_products 
LEFT OUTER JOIN t15_catalogue_line ON 
    (sc_products.unic = t15_catalogue_line.unic) 
LEFT OUTER JOIN price ON 
    (sc_products.unic = price.unic) 
    AND (sc_products.suupplier = price.postavchik) 
LEFT OUTER JOIN sc_group_discounts ON 
    (sc_products.item_group = sc_group_discounts.item_group) 
LEFT OUTER JOIN t10_item ON 
    (sc_products.unic = t10_item.unic and
    sc_products.CatText=t10_item.CatText and
    sc_products.brief_description_ru=t10_item.brand) 
LEFT OUTER JOIN t14_item_fields ON 
    (t10_item.T10_ITEM_GROUP = t14_item_fields.T14_ITEM_GROUP) 
WHERE 
    (
        sc_products.CatText = 'bracke mechanism' 
    or  
        sc_products.CatText='bracke montage ' 
    or 
        sc_products.CatText='hydraulic repair '
    )
    AND t15_catalogue_line.T15_CARTYPE = '30442' 
    AND t15_catalogue_line.T15_GROUP = '666' 
    and sc_products.unic is not null 
    and sc_products. unic!='' 

 GROUP BY 
     sc_products.product_code,sc_products.brief_description_ru,
     sc_products.suupplier ORDER BY ISNULL( price.price),
     price.price ASC