Oracle学习(六)——————————————查询基础

查询语句格式

select { [ distinct | all] columns | * }

from {tables | view |others select}

[where conditions]

[group by columns]

[having conditions]

[order by columns [asc | desc] ]

解释

[]表示可有可无

{}表示必须选择一个

where进行筛选

group by 对搜索进行分组显示

having 分组后的查询结果中筛选数据行

order by 对结果进行排序

这是我创建的表和表的内容



       SNO SNAME                                    SSEX           SAGE SDEPT
---------- ---------------------------------------- -------- ---------- ----------------------------------------
         6 李宇航                                   男               22 口腔医学
         7 苏小白                                   男               22 中医药
         1 张波                                     男               18 软件工程
         2 李增福                                   男               21 预防医学
         3 安霞                                     女               21 临床医学
         4 贺建                                     女               20 应用数学
         5 杨鹏飞                                   男               20 应用数学

已选择7行。

1、查询表中所有的列

格式:select * from 表名。

例子:select * from student;

SQL> select * from student;

       SNO SNAME                                    SSEX           SAGE SDEPT
---------- ---------------------------------------- -------- ---------- ----------------------------------------
         6 李宇航                                   男               22 口腔医学
         7 苏小白                                   男               22 中医药
         1 张波                                     男               18 软件工程
         2 李增福                                   男               21 预防医学
         3 安霞                                     女               21 临床医学
         4 贺建                                     女               20 应用数学
         5 杨鹏飞                                   男               20 应用数学

已选择7行。

2、查询表中的n列

格式:select 列名1,列名2... from 表名;

例子:select sno,sname from student;

SQL> select sno,sname from student;

       SNO SNAME
---------- ----------------------------------------
         1 张波
         2 李增福
         3 安霞
         4 贺建
         5 杨鹏飞
         6 李宇航
         7 苏小白

已选择7行。

3.消除重复行

关键字:distinct

作用消除重的行

select [distinct] ... from ..;

例子:

select ssex from student;


SQL> select ssex from student;

SSEX
--------
男
男
男
男
女
女
男

已选择7行。

select distinct ssex from student;

去重后:

SQL> select distinct ssex from student;

SSEX
--------
男
女

4.带有表达式的select子句

算术运算符 比如 + - * /

例子 select sage,sage*(1+1) from student;

SQL> select sage,sage*(1+1) from student;

      SAGE SAGE*(1+1)
---------- ----------
        22         44
        22         44
        18         36
        21         42
        21         42
        20         40
        20         40

已选择7行。

5.为列指定别名

关键字 as

格式:select 列名1 as "新列名1",列名2 as "新列名2" ...from 表名。

例子:select sno as "学号",sname as "姓名" from student;

SQL> select sno as "学号",sname as "姓名" from student;

      学号 姓名
---------- ----------------------------------------
         1 张波
         2 李增福
         3 安霞
         4 贺建
         5 杨鹏飞
         6 李宇航
         7 苏小白

已选择7行。

as关键字可以取消。

6.处理null

首先看一个不处理null的例子

SQL> select * from testnull;

         A          B
---------- ----------
         1
                    1
         1          2
         0          2
         1          0

这是刚创建一个表只有两列,都为number类型存五组数据

第一组的b和第二组的a没有存储数据为null,我们看两者相加的结果

SQL> select a as "加数",b as "加数",a+b as "和" from testnull;

      加数       加数         和
---------- ---------- ----------
         1
                    1
         1          2          3
         0          2          2
         1          0          1

我们会发现当有null时它们的和为空,所以我们这个时候需要处理null

解决办法:

a.nvl函数

格式:nvl(string1,replace_with);

如果string1不为null,返回string1,如果为空返回replace_with。replace_with也可以设置为null,这时返回的便是null.

使用nvl使两数相加

例子:select a as "加数",b as "加数",nvl(a,0)+nvl(b,0) as "和" from testnull;

SQL> select a as "加数",b as "加数",nvl(a,0)+nvl(b,0) as "和" from testnull;

      加数       加数         和
---------- ---------- ----------
         1                     1
                    1          1
         1          2          3
         0          2          2
         1          0          1

b.nvl2函数

也就时nvl的改进版

格式:nvl2(str1,str2,str3)

如果str1为null,返回str3的值,否则返回str2的值

7.连接字符串

a.关键字 ||

格式 select 列名1 || 'str' || 'str2' || .... from 表名;

例如:select '专业是'||sdept from student;

SQL> select '专业是'||sdept from student;

'专业是'||SDEPT
----------------------------------------------------------
专业是口腔医学
专业是中医药
专业是软件工程
专业是预防医学
专业是临床医学
专业是应用数学
专业是应用数学

已选择7行。

b.函数concat

格式:concat(str1,str2)

将str1和str2内容拼接

例子:select concat('专业是',sdept) from student;

SQL> select concat('专业是',sdept) from student;

CONCAT('专业是',SDEPT)
----------------------------------------------------------
专业是口腔医学
专业是中医药
专业是软件工程
专业是预防医学
专业是临床医学
专业是应用数学
专业是应用数学

已选择7行。

查询的基础就完成了,明天考科三,希望不要紧张,发挥正常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值