一:题目:

本题目要求编写SQL语句, 查询在具有最小内存容量的所有PC中具有最快处理器的PC制造商。

提示:请使用SELECT语句作答。

表结构:
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)

在这里插入图片描述

二:思路:

1:方式一:从条件到多表联合

分析:1.先拿内存进行分组,求出最小值 为表1
2.求出最小值的型号是多少 但要注意的是要控制住(其的ram为最小)为表2
3.将product和表2联合多表查询

2:方式二:从多表到条件

分析:这里是先将两个表联合起来,在根据条件进行筛选!

三:上码(最后的多表查询为最终结果)

1:方式一:

--1.求出最小值 取出第一行
select ram,max(speed) from PC
group by ram 
order by ram 
limit 0,1; 

-- 2.找出 有最小内存容量的所有PC中具有最快处理器 所对应的型号
select model
    from pc,(select ram,max(speed) as maxspeed from PC
              group by ram order by ram  limit 0,1) as temp
    where pc.speed = temp.maxspeed
    and pc.ram = temp.ram;
--3.多表联合查询    
select distinct maker
       from product,(select model
                            from pc,(select ram,max(speed) as maxspeed
                                            from pc
                                            group by ram order by ram  limit 0,1) as temp
                            where pc.speed = temp.maxspeed  and pc.ram = temp.ram) as a
       where product.model = a.model

在这里插入图片描述

方式2:

select maker
    from pc,product
    where pc.model = product.model
    and ram = (select min(ram) from pc)
    and speed = (select max(speed) from pc where ram = (select min(ram) from pc));


在这里插入图片描述

四:学习记录:

方式一第一种做法:

--1.求出最小值 取出第一行
select ram,max(speed) from PC
group by ram 
order by ram 
limit 0,1; 

-- 2.找出 有最小内存容量的所有PC中具有最快处理器 所对应的型号
select model
    from pc,(select ram,max(speed) as maxspeed from PC
              group by ram order by ram  limit 0,1) as temp
    where pc.speed = temp.maxspeed

--3.多表联合查询    
select distinct maker
       from product,(select model
                            from pc,(select ram,max(speed) as maxspeed
                                            from pc
                                            group by ram order by ram  limit 0,1) as temp
                            where pc.speed = temp.maxspeed ) as a
       where product.model = a.model

可以看到我在步骤二中,并未设置 pc.ram = temp.ram,,这样的后果是控制不住所查询出来的型号内存为最小,
比如:速度相同均为最快,但其内存不同,speed = 133 ram = 16;speed = 133 ram = 24; 如果不控制ram那么的话就会出现两个型号

五:总结:

sql语句逻辑性很强,每个人的逻辑不同,写出来的码也会不同,那么需要兄弟们,多验证数据,加油陌生人!!!!!!我们共勉!!!

Logo

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

更多推荐