3.Oracle基础一Oracle基础命令2


本文系列来自 FREE教程,稍加修改以方便复习SQL,如果冒犯请联系作者删除。

运算符

算术运算符

Oracle算术运算符包括+、-、*、/四个,其中/获得的结果是浮点数。

select a.*, b.coursename, c.stuname
  from score a, course b, stuinfo c
 where a.courseid = b.courseid
   and a.stuid = c.stuid;
 
select b.coursename, sum(a.score) / count(1)
  from score a, course b
 where a.courseid = b.courseid
   and a.courseid = 'R20180101'
 group by b.coursename;

关系运算符

Oracle关系运算符在where条件语句当中经常使用到分别为=,>,<

逻辑运算符

Oracle的逻辑运算符有三个:AND、OR、NOT。

字符串连接符||

Oracle中利用字符串连接符||(即双竖线)来连接查询结果。

select '姓名:' || c.stuname || ', 课程:' || b.coursename || ', 成绩:' || a.score || '分。' as sxcj
  from score a, course b, stuinfo c
 where a.courseid = b.courseid
   and a.stuid = c.stuid

去重DISTINCT

SELECT DISTINCT 列1,列2,列3... from 表名;

当关键字DISTINCT后面只有一个列1时,表示的是单个字段查询结果的不重复数据,当后面跟着多个列值时,表示的是多个字段组成的查询结果的所有唯一值,进行的是多个字段的分组消除。

询学生成绩表中课程“数学(2018上学期)”的所有出现的成绩,不重复
select distinct b.coursename, t.score
from score t, course b
where t.courseid = b.courseid
and t.courseid = ‘R20180101’;

条件查询

Oracle条件查询时经常使用=、IN、LIKE、BETWEEN…AND来作为条件查询的操作符。在Oracle select 查询中where条件经常使用到这几个操作符

=操作符

在条件查询语句中“=”表示列值等于一个固定值所查询出的结果。

 where t.stuid = b.stuid
   and t.courseid = 'R20180101'
   and t.score = '85'

IN操作符

在 Where 子句中可以使用 IN 操作符来查询其列值在指定的列表中的查询结果。

--利用逻辑运算符or 和条件"=" 查询
select t.stuid,
       t.courseid,
       t.score,
       b.stuname,
       b.sex,
       b.age,
       b.classno,
       b.grade
  from score t, stuinfo b
 where t.stuid = b.stuid
   and t.courseid = 'R20180101'
   and (t.score = '85' or t.score ='89' or t.score ='79');
-- 利用Oracle操作符”IN“查询  
select t.stuid,
       t.courseid,

   t.score,
       b.stuname,
       b.sex,
       b.age,
       b.classno,
       b.grade
  from score t, stuinfo b
 where t.stuid = b.stuid
   and t.courseid = 'R20180101'
   and t.score in ('85','89','79');

BETWEEN…AND

在 WHERE 子句中,可以使用 BETWEEN…AND 操作符来查询列值包含在指定区间内的查询结果 。

select t.stuid,
       t.courseid,
       t.score,
       b.stuname,
       b.sex,
       b.age,
       b.classno,
       b.grade
  from score t, stuinfo b
 where t.stuid = b.stuid
   and t.courseid = 'R20180101'
   and  t.score between '70' and '95'   --      >=70  and <=95

LIKE模糊查询

在Oracle条件查询where条件之中,当遇到查询值不清楚时,可以利用模糊查询LIKE关键字进行where条件的模糊查询。LIKE 关键字通过字符匹配检索出所需要的数据行。字符匹配操作可以使用通配符“%”和“_” :

1、%:表示零个或者多个任意字符。

2、_:代表一个任意字符。

3、\:指转义字符,“%”在字符串中表示一个字符“%”。

select * from STUINFO t where t.stuname like '张%';

Guess you like

Origin blog.csdn.net/weixin_43859562/article/details/112544916