我如何找到销售一种类型而不是另一种类型的制造商?

时间:2013-02-01 11:54:43

标签: mysql sql database

我必须:

  

找到销售PC而非笔记本电脑的制造商。

此查询未输出正确的结果:

SELECT maker, type FROM product
WHERE type = 'PC' AND type <> 'Laptop'

此输出和正确的输出:

output

此查询的结果集:

SELECT maker, type FROM product

ex2

表架构:

table

即便是这个也不起作用:

SELECT maker, type FROM product
WHERE type IN('PC') AND type NOT IN('Laptop')

注意 - 仅为了澄清而添加type

12 个答案:

答案 0 :(得分:7)

尝试:

SELECT maker
FROM product
GROUP BY maker
HAVING SUM(CASE WHEN type = 'PC' THEN 1 ELSE 0 END) > 0
   AND SUM(CASE WHEN type = 'Laptop' THEN 1 ELSE 0 END) = 0

答案 1 :(得分:4)

试试这个 找出销售PC而不是笔记本电脑的制造商。

SELECT maker , type FROM product
WHERE type = 'PC' AND maker NOT IN 
( SELECT maker FROM product WHERE type = 'laptop' )

答案 2 :(得分:3)

有许多奇特的方法可以解决这个问题。在这里,首先想到的是:)

  1. 微不足道的解决方案。

    SELECT DISTINCT maker
       FROM Product
         WHERE type = 'PC' AND maker NOT IN (
             SELECT maker
             FROM Product
             WHERE type = 'Laptop')
    
  2. 使用JOIN

    SELECT DISTINCT Pr.maker
    FROM Product AS Pr LEFT JOIN Product AS Pr2
        ON Pr.maker = Pr2.maker AND Pr2.type = 'Laptop'
    WHERE Pr.type = 'PC' AND Pr2.maker IS NULL
    
  3. 使用EXCEPT

    SELECT DISTINCT maker
        FROM product AS Pr1
        WHERE type = 'PC'
    EXCEPT
    SELECT DISTINCT Pr2.maker
        FROM product AS Pr2
        WHERE type = 'laptop'
    
  4. 使用DISTINCTNOT EXISTS

    SELECT DISTINCT maker
    FROM Product AS PcP
    WHERE type = 'PC' AND
          NOT EXISTS (SELECT maker
                      FROM Product
                      WHERE type = 'laptop' AND
                      maker = PcP.maker
                     )
    
  5. 使用CASE增量和HAVING

    SELECT maker
        FROM product
    GROUP BY maker
    HAVING SUM(CASE WHEN type = 'PC' THEN 1 ELSE 0 END) > 0
       AND SUM(CASE WHEN type = 'Laptop' THEN 1 ELSE 0 END) = 0
    
  6. 使用DISTINCT和比较子查询

    SELECT DISTINCT maker
    FROM Product AS Pr
    WHERE (SELECT COUNT(1)
           FROM Product AS Pt
           WHERE Pt.type = 'PC' AND Pt.maker = Pr.maker
          ) > 0 
        AND
          (SELECT COUNT(1)
           FROM Product AS Pt2
           WHERE Pt2.type = 'Laptop' AND
           Pt2.maker = Pr.maker
          ) = 0
    
  7. 使用HAVING并作弊(HAVING不能包含没有聚合的列,因此我们使用无意义(在本例中)聚合MIN(或{{1 - 无所谓)

    MAX

答案 3 :(得分:1)

select distinct maker 
from product 
where type = 'PC'  
  and maker not in ( select maker from product where type = 'Laptop')

它有效

答案 4 :(得分:1)

SELECT DISTINCT maker
FROM Product AS pc_product
WHERE type = 'pc' AND 
      NOT EXISTS (SELECT maker 
                  FROM Product
                  WHERE type = 'laptop' AND 
                        maker = pc_product.maker
                  );

答案 5 :(得分:0)

我可以建议您尝试修剪where子句中的值...可能存在一些差事空格,因此完全匹配不会返回所需的结果。

像这样:

SELECT maker , type FROM product
WHERE TRIM(type) = 'PC' AND TRIM(type) <> 'Laptop'

或者,您可以尝试:

SELECT maker , type FROM product
WHERE TRIM(type) = 'PC'

如果笔记本电脑和PC是两种单独的类型,那么只需过滤PC。

答案 6 :(得分:0)

尝试如下;

num = int(num)

答案 7 :(得分:0)

从产品中选择不同的制造商,其中type =&#39; PC&#39;并且制造商不在(从产品中选择制造商,类型=&#39;笔记本电脑&#39;)

答案 8 :(得分:0)

解决问题的简便方法

Select maker from product
where type = 'PC'
Except
Select maker from product
where type = 'laptop'

答案 9 :(得分:0)

select distinct Prd.maker as Maker
from   product as Prd INNER JOIN PC as P
ON     Prd.model = P.model
WHERE NOT EXISTS
(
  SELECT 1 FROM Laptop as L INNER JOIN Product as P
  ON L.model = P.model and Prd.maker = P.maker
)

答案 10 :(得分:0)

尝试此查询

SELECT DISTINCT MAKER FROM PRODUCT
WHERE TYPE IN ('PC') AND MAKER NOT IN
(SELECT MAKER FROM PRODUCT WHERE TYPE IN ('LAPTOP'))

答案 11 :(得分:-2)

select maker from Product where type in('PC')
intersect
select maker from Product where type  in ('PC','Printer')
except
select maker from Product where type='Laptop'