1.查询由生产厂商B生产的所有产品的型号(model) 和价格(price) (10 分)

-- 查询由生产厂商B生产的所有产品的型号(model) 和价格(price)。 
-- 提示:查询按照pc、laptop和printer的顺序进行。
-- union:是连接多个查询的语句
--   还有就是这里的 in ()  括号中不可添加为('1006','3002')

select model,price from pc
    where model in (select model from product where maker = 'B')
union   
select model,price from laptop
    where model in (select model from product where maker = 'B')
union    
select model,price from printer
    where model in (select model from product where maker = 'B')
    

2:查询所有出售便携式电脑(而不出售PC机)的生产厂商 (10 分)

-- 查询所有出售便携式电脑(而不出售PC机)的生产厂商。
-- 解释:个人电脑也就是pc机 laptop是便携式电脑

-- 分析:1.求出所有出售便携式电脑的厂商  (d,e)
--      2.求出所有出售pc机的厂商(a,d)
--      3.找出(d,e)不在(a,d)当中的当中的厂商

-- -- 便携式
select DISTINCT product.maker from laptop,product
            where laptop.model = product.model
            and product.maker not in (select maker from pc,product
                                where pc.model = product.model);

3:查询在两种或两种以上PC机上出现的硬盘容量 (10 分)

-- 查询在两种或两种以上PC机上出现的硬盘容量。

-- 分析:1.先按硬盘容量进行分组
--      2.在将其作为子表

-- select hd,count(*) from pc
--     group by hd;
    
select temp.hd
    from (select hd,count(*) as num from pc
            group by hd) temp
    where temp.num >= 2;
    

4:查询拥有相同速度和内存的PC机的成对的型号 (10 分)

select temp1.model as model1,temp2.model as model2
    from pc temp1,pc temp2
    where temp1.speed = temp2.speed
    and temp1.ram = temp2.ram
    and temp1.model < temp2.model

5. 查询电影“M3”中的男影星 (10 分)

-- 多表查询  条件是 演员的姓名进行联系两个表
--   然后就是 演员表当中性别为男

-- select name
--      from StarsIn,MovieStar
--      where starName = name
--      and gender = 'M'
--      and movieTile = 'M3';


--  用到了多行子查讯
-- select starName from MovieStar
--     where movieTile = 'M3';
    
select name
     from MovieStar
     where name in (select starName from StarsIn
                        where movieTitle = 'M3')
     and gender = 'M';                 

6:查询st1制片公司的总裁 (10 分)

select MovieExec.name
      from MovieExec,Studio
      where certID = presCertID
      and Studio.name = 'st1';

7:`查询在st1公司于2018年制作的电影中出演的影星 (10 分)

--  查询在st1公司于2018年制作的电影中出演的影星。

-- 分析:1.查询st1公司2018年制作了哪些电影在movie表当中
--      2.然后在将其上方的查询结果作为子查询 在StarsIn表中

-- select title from Movie
--     where studioName = 'st1'
--     and year = 2018;
    

SELECT  DISTINCT starName
        FROM StarsIn
        WHERE movieTitle IN (SELECT title FROM Movie
                            WHERE studioName = 'st1'
                              AND YEAR = 2018)	
        and movieYear = 2018;

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐