一个字段同时满足多个条件的查询

构造一个如下图所示的表:

一个字段同时满足多个条件的查询_第1张图片

№_001: 创建表 tb_a

DROP TABLE tb_a CASCADE CONSTRAINT;
COMMIT;
CREATE TABLE tb_a(
model_name varchar2(10),
parts_name varchar2(15));
COMMIT;

№_002: 导入数据

INSERT INTO tb_a (model_name, parts_name) VALUES ('飞机','轮胎');
INSERT INTO tb_a (model_name, parts_name) VALUES ('飞机','座椅');
INSERT INTO tb_a (model_name, parts_name) VALUES ('飞机','机翼');
INSERT INTO tb_a (model_name, parts_name) VALUES ('飞机','玻璃');
INSERT INTO tb_a (model_name, parts_name) VALUES ('汽车','轮胎');
INSERT INTO tb_a (model_name, parts_name) VALUES ('汽车','座椅');
INSERT INTO tb_a (model_name, parts_name) VALUES ('汽车','玻璃');
INSERT INTO tb_a (model_name, parts_name) VALUES ('自行车','轮胎');
INSERT INTO tb_a (model_name, parts_name) VALUES ('自行车','座椅');
INSERT INTO tb_a (model_name, parts_name) VALUES ('自行车','方向盘');
COMMIT;

查询要求:

查询含有“轮胎”、“座椅”、“玻璃” 三个配件的设备

SELECT x.model_name
FROM tb_a x
WHERE x.parts_name IN ('轮胎','座椅','玻璃') --限制三个配件
GROUP BY x.model_name
HAVING COUNT(*)=3 --关键语句


转载于:https://www.cnblogs.com/veygoux/p/4181499.html

你可能感兴趣的:(一个字段同时满足多个条件的查询)