一:题目
本题目要求编写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;