多语言展示
当前在线:1359今日阅读:103今日分享:49

oracle基础查询

oracle基础查询
方法/步骤
1

语法 select * | 字段1,字段2 from 表名;查询使用别名 查询的时候  表名   字段名    都不区分大小写    但表中列的内容区分大小写 oracle字段别名  可以直接 使用   例如  年薪   也可以 使用  '年薪'  也可以直接 使用  as 年薪   但不能使用单引号    但是别名尽量  别用 中文 select id as '序号',name as '姓名',mymoney as '工资' from student;

2

查询去除重复 distinct select distinct 字段1,字段2 from emp order by id;  如果是去除两个字段的重复,那么两个字段都必须要满足重复的条件才可以去除

3

向表中插入日期数据 格式为   UPDATE 'MYCHOY'.'STUDENT' SET BIRTH = TO_DATE('2017-12-29 01:04:46', 'YYYY-MM-DD HH24:MI:SS') WHERE ROWID = 'AAASNTAAGAAAACFAAA'

4

to_char是把数据库内的数据格式在查询的时候进行一个转换 比如查询1988年以后入职的员工或者是6月份入职的职工 select name,mymoney from student where to_char(birth,'yyyy')>'1988'; select name,mymoney from student where to_char(birth,'mm')>'6';

5

where条件控制 关系运算符 >  <  >=  <=  =  !=   <>   !=和 <>的效果是一样的  范围运算符  betwenn..A..and..B.   验证值是否在范围之内  包含A  B 两个 值 在内的数据 谓词范围   in   验证操作数  in(200,600)   验证 200   600这两个值是否在表中  只查询出 满足条件200   600 这两个值 的数据 not in  不在指定范围的查询   在使用not in操作符的时候范围内的值不能为nullselect *  from emp where sal in (5930,4895,null);   否则返回的是数据为空  不会有数据显示 逻辑运算符    and 两个条件必须都满足     or  两个条件满足一个即可 not  空判断    is null     is   not null比如:is null  是否是null的查询select * from student where mymoney is null;

6

like  模糊查询 %  表示任意0到多个字符     查询首字母为n的用户的工资和姓名 select name ,mymoney from student where name like 'n%'; _  表示任意单个字符 查询第三个字符为n的用户的姓名和工资 select name ,mymoney from student where name '__n%';

7

order by  排序  对查询结构进行排序 select * from student where mymoney<6600 order by mymoney desc/asc; 按id 升序    mymoney  降序 select * from student order by id asc,mymoney desc;

8

最大  最小  平均  总和   总条数 max   min   avg   sum  count    avg会忽略为空的数据  不会统计为null的行 最高工资   高低工资    平均工资   工资总和    员工总数 select max(mymony) from student; select min(mymony) from student; select max(mymony),min(mymony) from student; select avg(mymony),sum(mymony) from student; 平均工资需要这样 select sum(mymony)/count(*) from student;

9

子查询 查询出工资最高的姓名   工资  序号 select id as 序号,name as 姓名,mymoney as 工资 from student where mymoney=(select max(mymoney) from student);在子查询中  如果子查询的结果中有null值的出现  那么     整个查询语句查询出来的结果会是空的   select * from myemp where sal in (select sal from myemp where sal>3000);select * from myemp where sal in(select sal from myemp);  这个子查询的结果是有空值的   那么这个查询返回的结果是没有数据的     想要避免这个情况的出现   要使用   any来代替 in来进行子查询any分为三种形式=any  与in操作一样    select * from myemp where sal =any(select sal from myemp where job='MANAGER');>any   比子查询返回的最小值要大的数据select * from myemp where sal >any(select sal from myemp where job='MANAGER');all   比子查询返回的最大值要大的数据

10

group by   也称为分组查询 用于对查询的结果分组统计 示例:查询每个部门的最高工资和平均工资统计函数统计个数 count(*|字段)         count(字段)  不统计为空的字段的个数       count(*)统计所有数据的条数  是最准确的    count(distinct字段) 统计去除重复字段的数据条数min(字段)max(字段)sum(数字字段)avg(数字字段) select (sum(mymoney)/count(*)),max(mymoney),bumen from student group by bumen; 示例:查询每个部门的每种岗位的最高工资和平均工资 select (sum(mymoney)/count(*)),max(mymoney),bumen,gangwei from student group by bumen,gangwei order by bumen;示例   查询 出每个部门的平均工资和平均人数   部门有重复的 要先去除重复select (sum(sal)/count(*)) as csal,job,(count(*)/count(distinct job)) as hrdate from myemp group by job;

推荐信息