查询

查询语句不区分大小写(不包括引号内字符串)

查询可以分多行编写,但是不能自由拆分关键词或者对象名称换行

需要添加分号“;”结尾

单行注释:--注释内容

多行注释:/注释内容/

--基础语法

select id, name from student;
--select, from:语句关键词
--id, name:字段
--student:数据表--查询出“学生”表中学生的ID和姓名

--别名

--用于指定查询的字段来自于哪张表,多用于表关联时
select t.id, t.name as stuname from student t;
--t:表别名,可以是任意符合sql对象命名规则的名称,如:s、_s、s1
--as:字段别名,用于重命名字段,name>>stuname

--条件查询

select t.id, t.name
  from student t
 where t.birthday >= date'2020-01-01';
--where:语句关键词,表示条件
--查询出“学生”表中生日在2020-01-01及之后的学生的ID和姓名

--多个条件查询

select t.id, t.name
  from student t
 where t.birthday >= date'2020-01-01'
   and t.gender = '男';

--逻辑关键词

--    and:且
--    or:或
--    not:否
--关系符号、运算符、操作符:
--    =
--    >
--    <
--    >=
--    <=
--    <>:不等于
--    +
--    -
--    *
--    
--    ||:连接,字符串拼接
--    is null:为空
--    is not null:不为空
--    exists:存在,用于条件中存在...的情况
--    in:在某个范围内

--查询出“学生”表中生日在2020-01-01及之后的男学生的ID和姓名

--多条件查询
select t.id, t.name
  from student t
 where (t.birthday >= date'2020-01-01'
   and t.gender = '男')
    or (t.birthday >= date'2019-01-01'
   and t.gender = '女');
--查询出“学生”表中生日在2020-01-01及之后的男学生,或者生日在2019-01-01及之后的女生,查询他们的ID和姓名

--多条件查询

select t.id, t.name
  from student t
 where t.birthday >= date'2020-01-01'
   and t.gender is null
   and not (t.birthday >= date'2021-01-01')
--查询出“学生”表中生日在2020-01-01及之后、性别数据为空的、生日不在2021-01-01及之后的学生的ID和姓名

--多条件查询,exists,子查询

select t.id || '_' || t.name as nname
  from student t
 where t.gender = '男'
   ane exists (select 1 from student s
                where t.name = s.name
                  and s.gender = '女');
--查询存在同名女生的男生ID和姓名,拼接成“001_张三”这样的格式生成新的数据

--子查询,in

select t.id, t.name
  from student t
 where t.id in (select s.id from student2 s);
 --查询t表具有的s表相同id的数据

--模糊匹配

select t.id, t.name
  from student t
 where t.name like '%天%';
--查询姓名中含有“天”的学生
--like:模糊匹配关键词
--    %:可以用来代替任意字符,“like '%王'”:以王结尾,like '王%':以王开头
--    _:可以用来代替任意一个字符,“like '_王_'”:第二个字符是王,共三个字符
--    \:转义字符,如果要单纯的使用下划线或者百分号,使用“\_”和“\%”

--判断 decode

select t.id,
       t.name,
       decode(t.gender, '男', 1, '女', 0, null) as gender_flag
  from student t;
--判断t.gender,为“男”时显示1,为“女”时显示0,类推,判断条件和条件结果均为成对出现,最后一个单独的参数为不满足之前所有条件的显示结果

--判断 case when

select t.id,
       t.name,
       case
         when t.gender = '男' then
           1
         when t.gender = '女' then
           0
         else
           null
       end as gender_flag
  from student t;
--顺序判断when

--排序

select t.id,
       t.name
  from student t
 order by t.birthday;
--按生日排序(默认升序)

--排序

select t.id,
       t.name
  from student t
 order by t.classi asc, t.birthday desc;
--按班级升序,生日降序排序

--函数

select count(1) as cou
  from student t;
--查询总的学生数

--分组函数

select t.classi,
       t.gender,
       count(1) as cou
  from student t;
 group by t.classi, t.gender;
--按班级和性别查询学生数

--函数

select t.id,
       t.name,
       substr(t.name, 1, 1) as name1
  from student t;
--截取名字的第一个字
--函数分类很多种
--常用的单行函数
--    lenght:计算字符长度
--    upper/lower:字符串大写、小写
--    substr:截取
--    instr:判断字符串是否包含
--    lpad/rpad:补位
--    trim/ltrim/rtrim:截取
--    replace:替换
--    nvl/nvl2/nullif/coalesce:空值处理
--    to_char/to_number/to_date:字段类型转换
--    abs:数字绝对值
--    sign:数字符号
--    floor:向下取整数
--    trunc/round:精度控制,截取/四舍五入,正对数字或者日期
--    sysdate:当前系统日期
--    add_months:日期加月
--    last_day:当月最后一天
--    next_daye:下一天
--    months_between:计算月间隔
--常用的聚合函数,配合group by使用
--    sum:求和
--    avg:平均
--    count:计数
--    wm_concat:拼接

DUAL

空值处理(null;nvl、nvl2、nullif、coalesce)

去重(distinct)

结果合并、交并差(union、union all、minus、intersect)


DUAL

一个任意宽的单行虚拟表,本身并没有意义,用来构建语法:

空值处理

空值,null,是一个无意义的不存在的值,不是0,也不是空格,只是“空”。

提供了下面的简单示例:

nvl,两个参数,当第一个参数为空则返回第二个参数,第一个参数不为空返回第一个参数:

select nvl(null, 2) from dual;
select nvl(1, 2) from dual

nvl2 ,三个参数,当第一个参数为空则返回第三个参数,第一个参数不为空返回第二个参数:

select nvl2(null, 1, 2) from dual;
select nvl2(1, 1, 2) from dual;

nullif ,两个参数,如果两个参数相等,则返回空,如果不等,返回第一个参数:

select nullif(1, 1) from dual;
select nullif(1, 2) from dual;

coalesce ,可以有很多参数,取参数中第一个不为空的值:

select coalesce(null, null, 1) from dual

另外,由于空值的特殊性,在增删改查的过程中需要稍微注意一下,官方解释:

https://docs.oracle.com/en/database/oracle/oracle-database/20/sqlrf/Nulls.html#GUID-B0BA4751-9D88-426A-84AD-BCDBD5584071

用一个表格说明(null 不用等于或不等于,用的是“is”、“is not”):

ConditionValue of AEvaluation
a IS NULL10FALSE
a IS NOT NULL10TRUE
a IS NULLNULLTRUE
a IS NOT NULLNULLFALSE
a = NULL10UNKNOWN
a != NULL10UNKNOWN
a = NULLNULLUNKNOWN
a != NULLNULLUNKNOWN
a = 10NULLUNKNOWN
a != 10NULLUNKNOWN

补充:聚合函数针对没有值的数据返回空(子查询结果是没有行的):

去重

distinct,去除重复行的意思,可以是多个字段**
**

结果合并、交并差

两个结果集的合并,两个结果集合需要有相同的字段和字段类型

union ,并集,去除重复行:

union all ,不去除重复行:

minus ,差,可以理解为减法:

intersect ,可以理解为交集:

最后修改:2021 年 10 月 25 日
如果觉得我的文章对你有用,请随意赞赏