1. oracle中把有null值的字段当做where查询条件时,有null的数据会返回false
- 原数据:
SELECT * FROM GGS."student02"
- null值的字段当做where条件:
SELECT * FROM GGS."student02" a WHERE exists (SELECT * FROM GGS."student02" b WHERE a."age" =b."age" AND a."name" = b."name")
- 同样一张表,age = null 的数据没有了
2.使用函数:nvl(字段,值),当指定字段为null时,以值代替null进行比较
SELECT * FROM GGS."student02" a WHERE exists (SELECT * FROM GGS."student02" b WHERE NVL(a."age", 0) = NVL(b."age", 0) AND a."name" = b."name")
- 测试数据
CREATE TABLE "GGS"."student02" (
"name" VARCHAR2(255 BYTE) ,
"age" NUMBER
);
INSERT INTO "GGS"."student02" VALUES ('李四', '21');
INSERT INTO "GGS"."student02" VALUES ('王五', '22');
INSERT INTO "GGS"."student02" VALUES ('张三', NULL);
INSERT INTO "GGS"."student02" VALUES ('赵六', NULL);
INSERT INTO "GGS"."student02" VALUES ('张三', '20');