6

我必须:

查找销售个人电脑但不销售笔记本电脑的制造商。

此查询未输出正确结果:

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

此输出和正确输出:

输出

此查询的结果集:

SELECT maker, type FROM product

ex2

表架构:

桌子

即使这个也不起作用:

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

注意 -type添加只是为了澄清

4

12 回答 12

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
于 2013-02-01T12:00:04.747 回答
6

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

  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(或MAX- 没关系)

    SELECT maker
        FROM (SELECT DISTINCT maker, type
              FROM Product
              WHERE type IN ('PC', 'Laptop')
             ) AS T
    GROUP BY maker
    HAVING COUNT(*) = 1 AND MIN(type) = 'PC'
    
于 2016-01-22T09:27:26.547 回答
4

试试这个 找出销售个人电脑但不销售笔记本电脑的制造商。

SELECT maker , type FROM product
WHERE type = 'PC' AND maker NOT IN 
( SELECT maker FROM product WHERE type = 'laptop' )
于 2013-02-01T12:02:45.137 回答
1
select distinct maker 
from product 
where type = 'PC'  
  and maker not in ( select maker from product where type = 'Laptop')

有用

于 2013-06-04T08:25:17.690 回答
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
                  );
于 2015-05-29T19:46:58.867 回答
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 是 2 种不同的类型,则只需过滤 PC。

于 2013-02-01T11:59:45.650 回答
0

尝试如下;

    Select maker from product where type in('PC','laptop')
    except
    select maker from product where type='laptop'
于 2015-12-01T05:21:15.513 回答
0

从 type='PC' 和 maker 不在的产品中选择不同的制造商(从 type='laptop' 的产品中选择制造商)

于 2015-12-02T00:35:33.897 回答
0

解决问题的简单方法

Select maker from product
where type = 'PC'
Except
Select maker from product
where type = 'laptop'
于 2016-06-14T22:10:56.790 回答
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
)
于 2017-10-06T07:36:08.077 回答
0

试试这个查询

SELECT DISTINCT MAKER FROM PRODUCT
WHERE TYPE IN ('PC') AND MAKER NOT IN
(SELECT MAKER FROM PRODUCT WHERE TYPE IN ('LAPTOP'))
于 2017-10-30T21:38:25.413 回答
-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'
于 2015-08-19T07:11:56.143 回答