MySQL SQL选择具有多个子选择的查询

时间:2015-03-09 12:43:08

标签: mysql sql subquery

好的,所以这个人让我把头发撕了好几个小时。我觉得我有一些明显的东西可以忽略。

我有2个表,服务品牌

service
-------
id
brand

brand
-----
id
brandName

所以service.brand可以是以下任何一种:

Blank 
"Other" 
Integer (matches brand.id) 
String (matches brand.brandName)
String (Not Blank, Not Other, Not brand.brandName)

我正在尝试编写一个查询,从品牌表中提取正确的品牌名称,如果service.brand的值不在brand.id或brand.brandName中,则显示其中的任何内容。

到目前为止,我已经完成了所有工作,但如果service.brand不是空白​​,不是其他,不是brand.id,而不是brand.brandName,那么它不会提取记录。 (我从这里开始称之为OtherThanOther)。

现在我的最新尝试大部分是正确的,但是OtherThanOther字段会被拉多次,就像总记录是40,OtherThanOther是相同的记录差不多20次。帮助!

我最近的尝试..

select 
    s.*, b.brandName as bname 
from 
    service s, brand b 
where 
    s.brand = b.brandName 
or 
    s.brand = b.id 
or 
    s.brand = 'Other' 
or 
    s.brand = ''
or
    (       
        s.brand not in (select brandName from brand)
        and
        s.brand not in (select id from brand)
        and
        s.brand != 'Other'
        and
        s.brand != ''
    )

样本表数据

service
-------
1 5
2 Dell
3 SomeRandom
4 
5 Other

brand
-----

1 HP
2 Gateway
3 Dell
4 Compaq
5 Toshiba

我的查询结果..

(service.id, service.brand, brand.id, brand.brandName, bname)
-------------------------------------------------------------
1 5 5 Toshiba Toshiba
2 Dell 3 Dell Dell
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
4 '', null, null, null
5 Other, null, null, null

我需要它只需拉一次SomeRandom,group by将无效,因为可能有多个字段与SomeRandom具有相同的值。 非常感谢任何帮助。

谢谢!

4 个答案:

答案 0 :(得分:1)

select 
    s.*, 
    CASE 
      WHEN b_id.brandName IS NOT NULL THEN b_id.brandName
      WHEN b.brandName IS NOT NULL THEN b.brandName
      ELSE s.brand 
    END as bname 
from 
    service s
LEFT JOIN brand b_id 
ON  CAST(s.brand AS UNSIGNED) = b_id.id
LEFT JOIN brand b
ON  s.brand = b.brandName

答案 1 :(得分:0)

您使用的是旧式inner join

from service s, brand b 
where s.brand = b.brandName or ...

更清楚地写成:

from service s
inner join brand b on s.brand = b.brandName or ... 

在较新的形式中,问题立即脱颖而出。 inner join会过滤掉不匹配的行。改为使用left join

from service s
left join brand b on s.brand = b.brandName or ...

现在您将获得所有服务,即使在brand表中找不到匹配的行。

答案 2 :(得分:0)

您应该JOIN 2个包含多个条件的表格。像这样:

SELECT s.*, b.brandName AS bname
FROM service AS s
INNER JOIN brand AS b
ON s.brand = b.brandName OR 
   s.brand = b.id OR 
   s.brand = 'Other' OR 
   s.brand = '' OR  
   (s.brand NOT IN (SELECT brandName FROM brand) AND
   s.brand NOT IN (SELECT id FROM brand) AND 
   s.brand != 'Other' AND
   s.brand != '') 

答案 3 :(得分:0)

首先 - 使用JOIN!

select s.*, b.brandName as bname 
from service s 
    LEFT JOIN brand b ON s.brand = b.brandName or s.brand = b.id

然后您可以添加您的提示条件 - 例如WHERE NOT s.brand IS NULL

您的这些条件导致您的表格行相互相乘的结果:

s.brand = 'Other' 
or 
    s.brand = ''
or
     (       
        s.brand not in (select brandName from brand)
        and
        s.brand not in (select id from brand)
        and
        s.brand != 'Other'
        and
        s.brand != ''
    )