10-2 5-2 查询至少生产两种不同的计算机(PC或便携式电脑)且机器速度至少为133的厂商 (20 分)(思路加详解+测试用例)

一:题目

本题目要求编写SQL语句, 查询至少生产两种不同型号的计算机(PC或便携式电脑)且机器速度至少为133的厂商,输出结果按照制造商升序排列。

提示:

本题有三种情况: (1) 至少生产两种不同PC的厂商 (2) 至少生产两种不同便携式电脑的厂商 (3) 至少生产一种PC且至少生产一种便携式电脑的厂商。

表结构:

CREATE TABLE product
( maker CHAR(20) ,          --制造商
  model CHAR(20) NOT NULL,  --产品型号
  type CHAR(20),            --产品类型
  PRIMARY KEY(model)
);
CREATE TABLE pc
( model CHAR(20) NOT NULL,    --型号
  speed  DECIMAL(6,2),        --速度
  ram  INT,                   --内存
  hd DECIMAL(6,2),            --硬盘容量
  cd CHAR(4),                 --光驱
  price INT,                  --价钱
  PRIMARY KEY(model),
  FOREIGN KEY(model) REFERENCES product(model)
);
CREATE TABLE laptop
( model CHAR(20) NOT NULL,    --型号
  speed  DECIMAL(6,2),        --速度 
  ram  INT,                   --内存
  hd DECIMAL(6,2),            --硬盘容量
  screen DECIMAL(6,2),        --屏幕大小
  price INT,                  --价钱
  PRIMARY KEY(model),
  FOREIGN KEY(model) REFERENCES product(model) 
);

在这里插入图片描述

二:测试用例

吐槽一下,pta上关于sql语句没提供测试用例的地方,着实让人讨厌,但是贴心杰,自己写了个测试用例来测试自己的码,现在分享给大家 帮助大家来测试自己的码

CREATE TABLE product
( maker CHAR(20) ,          -- 制造商
  model CHAR(20) NOT NULL,  -- 产品型号
  TYPE CHAR(20),            -- 产品类型
  PRIMARY KEY(model)
);
CREATE TABLE pc
( model CHAR(20) NOT NULL,    -- 型号
  speed  DECIMAL(6,2),        -- 速度
  FOREIGN KEY(model) REFERENCES product(model)
);
CREATE TABLE laptop
( model CHAR(20) NOT NULL,    -- 型号
  speed  DECIMAL(6,2),        -- 速度 
  FOREIGN KEY(model) REFERENCES product(model) 
);


INSERT INTO product
	VALUES('D','2001','便携式电脑'),
	('D','2002','便携式电脑'),
	('E','2004','便携式电脑'),
	('D','3001','打印机'),
	('B','3002','打印机'),
	('A','1001','个人电脑'),
	('A','1002','个人电脑'),
	('A','1003','个人电脑'),
	('D','1008','个人电脑');
	
	
SELECT * FROM product;	

INSERT INTO pc
	VALUES('1001',133),
	('1002',120),
	('1003',166),
	('1008',180);
	

	
	
SELECT * FROM pc;

INSERT INTO laptop
	VALUES('2001',100),
	('2002',133),
	('2004',133);

SELECT * FROM laptop;

直接复制运行就可以

二:分析

– 查询至少生产两种不同型号的计算机(PC或便携式电脑)且机器速度至少为133的厂商,输出结果按照制造商升序排列。
– (1) 至少生产两种不同PC的厂商
– (2) 至少生产两种不同便携式电脑的厂商
– (3) 至少生产一种PC且至少生产一种便携式电脑的厂商。

– 分析:
1.将pc和product表联合查询设置条件 速度大于 133 查询出来的 结果有多个厂商,表1
2.将表1按maker进行分组,统计每个厂商的个数,筛选出大于等于2的厂商 (可以得到厂商和其个数)表2
3.laptop 和 product 和上述过程一样 表
4.针对体条件三,我们先将pc和product联合查询得到表4,将laptop和product表联合查询 并将表4作为条件 即可满足条件三
5.将表2和表3表4联合起来 并按maker排序

四:上码

-- 查询至少生产两种不同型号的计算机(PC或便携式电脑)且机器速度至少为133的厂商,输出结果按照制造商升序排列。
-- (1) 至少生产两种不同PC的厂商 
-- (2) 至少生产两种不同便携式电脑的厂商 
-- (3) 至少生产一种PC且至少生产一种便携式电脑的厂商。


-- 分析:1.将pc和product表联合查询设置条件 速度大于 133  查询出来的 结果有多个厂商,表1
--      2.将表1按maker进行分组,统计每个厂商的个数,筛选出大于等于2的厂商 (可以得到厂商和其个数)表2
--      3.laptop 和 product 和上述过程一样 表3
--      4.针对体条件三,我们先将pc和product联合查询得到表4,将laptop和product表联合查询 并将表4作为条件 即可满足条件三
--      5.将表2和表3表4联合起来 并按maker排序

-- 1.
-- select  maker
--         from pc,product
--         where pc.model = product.model
--         and pc.speed >= 133;
        
-- -- 2.
-- select maker, count(maker)
--       from (select  maker
--                     from pc,product
--                     where pc.model = product.model
--                     and pc.speed >= 133) as temp1
--       group by maker  having count(maker) >= 2;            

-- -- 3.
-- select  maker
--         from laptop,product
--         where laptop.model = product.model
--         and laptop.speed >= 133;
        
 
--  select maker, count(maker)
--       from (select  maker
--                     from laptop,product
--                     where laptop.model = product.model
--                     and laptop.speed >= 133) as temp2
--       group by maker  having count(maker) >= 2;  
      
-- 4.  
-- select distinct maker
--        from laptop,product
--        where laptop.model = product.model
--        and  product.maker in (select maker
--                                         from pc,product
--                                         where pc.model = product.model);

select distinct maker 
    from
        (select  maker
                from
                    (select maker, count(maker)
                          from (select  maker
                                        from pc,product
                                        where pc.model = product.model
                                        and pc.speed >= 133) as temp1
                          group by maker  having count(maker) >= 2     
                    union     
                    select maker, count(maker)
                          from (select  maker
                                        from laptop,product
                                        where laptop.model = product.model
                                        and laptop.speed >= 133) as temp2
                          group by maker  having count(maker) >= 2) as temp3
        union
        select maker
               from laptop,product
               where laptop.model = product.model
               and  product.maker in (select maker
                                            from pc,product
                                            where pc.model = product.model)) as temp4
      order by maker;          
         
         
         
         
         
        

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天向上的菜鸡杰!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值