10-4 6-4 查询厂商“D“生产的PC和便携式电脑的平均价格 (10 分)思路+详解+测试用例

前言:测试用表

贴心杰将这个测试表分享给大家 ,如果大家做题的时候发现那个点过不去,一定不要直接看别人的博客,先自己测试用例,如果思路也对 ,验证数据也对,还有错误 你再看看别人的思路!!!

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) 
);


INSERT INTO product
	VALUES('D','2001','便携式电脑'),
	     ('D','2002','便携式电脑'),
	     ('D','3001','打印机'),
	     ('D','1008','个人电脑'),
	     ('D','1009','个人电脑');
	     
INSERT INTO product
	VALUES('A',1001,'个人电脑'),
	('A',1002,'个人电脑');
	     
	     
	     
INSERT INTO pc
	VALUES('1008',180.00,32,2.00,'8X',3699),
	      ('1009',200.00,32,2.50,'8X',2599);
	      
INSERT INTO pc
	VALUES('1001'	,133.00,	16,	1.60,	'6X',	1595),
	('1002',	120.00,	16	,1.60,	'6X',	1399);
	      
	      
INSERT INTO laptop
	VALUES('2001',100.00,20,1.10,9.50,1999),
	(2002,117.00,12,0.75,11.30,2499);
	     

一:10-4 6-4 查询厂商"D"生产的PC和便携式电脑的平均价格 (10 分)

-- 查询厂商D生产的PC和便携式电脑的平均价格

-- 分析:1.查询出D和PC一个表 字段为price
--      2.查询D和laptop一个表 字段为price
--     3.union all 两个表 然后求出平均值和 注意 union all 是不去重的  union 是去重的
--     4.还有就是这道题最后求取的结果是要四舍五入的  要用 round(平均值) 别问我怎么知道的,我是试出来的 先用的floor 不对
--       然后改成 round 就对了
--    


-- 1.
-- select price
--     from product,pc
--     where product.model = pc.model;
    
-- -- 2.
-- select price
--     from product,laptop
--     where product.model = laptop.model;
    
-- 3.
SELECT round(AVG(price)) AS avg_price
    FROM (
    SELECT price
        FROM product,pc
        WHERE product.model = pc.model
        AND maker = 'D'
    UNION ALL
    SELECT price
        FROM product,laptop
        WHERE product.model = laptop.model   
        AND maker = 'D'
    ) temp;
	     

二:10-3 6-3 查询厂商"A"生产的PC的平均价格 (10 分)

-- 查询厂商A生产的PC的平均价格。

-- 分析:1.查询出A生产的PC
--      2.求其平均价格

select avg(price) as avg_price
    from product,pc
    where product.model = pc.model
    and maker = 'A';

三:10-6 6-6 查询各厂商生产的便携式电脑的显示器平均尺寸 (10 分)

--  查询各厂商生产的便携式电脑的显示器平均尺寸。
--  分析:1.先求出各个厂商对应的电脑屏幕
--       2.将其作为子表进行分组查询 平均尺寸;


-- 错误示例:聚合函数不能和字段一块用  当 不是group by 的时候
-- select maker,avg(screen) as avg_screen
--     from product,laptop
--     where product.model = laptop.model;

-- -- 1.
-- select maker,screen
--     from product,laptop
--     where product.model = laptop.model;
    
    
-- 2.
select maker,avg(screen) as avg_screen
    from (select maker,screen
                from product,laptop
                where product.model = laptop.model) temp
     group by maker;


四:10-7 6-7 查询生产三种不同型号的PC的厂商 (10 分)

-- 查询生产三种不同型号的PC的厂商。
-- 分析:1.先将pc表和product表联合起来 字段为厂商 和 型号 这样求出 每个厂商对应的pc型号  表1
--      2.对表1进行分组查询 字段为 厂商 和 型号个数 并筛选出数量为3的厂商  表2
--      3.对表2进行查询 

-- -- 1.
-- select maker,pc.model
--     from pc,product
--     where pc.model = product.model
   
-- -- 2
-- select maker,count(maker) as nums
--     from (select maker,pc.model
--                 from pc,product
--                 where pc.model = product.model) temp
--      group by maker  having nums = 3;     
     
-- -- 3
    select maker
        from (select maker,count(maker) as nums
                    from (select maker,pc.model
                               from pc,product
                               where pc.model = product.model) temp
                     group by maker having nums = 3) temp1         

五:10-10 6-10 查询所有生产打印机的厂商生产的PC的硬盘平均容量 (10 分)

-- 查询所有生产打印机的厂商生产的PC的硬盘平均容量。

-- 分析:
--     1.查询出既生产 打印机 又生产 PC 的厂商    
--       1>.查询出生产打印机的厂商 表1
--       2>.查询product表 字段为 厂商,model,条件为 maker 来自 表1 and type = 个人电脑 表2
--    2.联合表2和PC表求出 平均硬盘容量

-- 1.1
-- select maker
--     from product,printer
--     where product.model = printer.model
    
-- -- 1.2
-- select maker,product.model
--     from product
--     where maker in (select maker
--                         from product,printer
--                         where product.model = printer.model)
--     and type = "个人电脑"
    
    
-- 2
select avg(hd) as avg_hd
    from pc,(select maker,product.model
                    from product
                    where maker in (select maker
                                        from product,printer
                                        where product.model = printer.model)
                    and type = "个人电脑") temp
    where pc.model = temp.model;                



其他 有疑问的题可以留言 , 上方分享一下我认为很好的题!!!!!!!!!
写sql语句 一定要有自己的分析,然后一步步写写,最重要的你的逻辑表达出来,这几道破题,不是最重要的
好了 有疑问的题可以留言 或择你认为有难度的题也可以分享给我 ,我非常乐意做题,加油陌生人

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天天向上的菜鸡杰!!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值