Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example of getting started with oracle basic query

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article describes the basic query oracle examples usage. Share it for your reference, as follows:

Basic select statements

SELECT *|{[DISTINCT] column|expression [alias], ...} FROM table;

For example:

--Query all data select * from emp;--Query specified column data select empno,ename,sal from emp;--Arithmetic operator (+ - * /)select ename,sal,sal+30 from emp;--Use parentheses select ename,sal,12*(sal+30) from emp;--Define null value--(null is invalid, unspecified, unknown or unpredictable, null is not a space or 0)select ename,job,sal,comm from emp;--null mathematical operations--null mathematical expressions all have null values select ename,12*sal+comm from emp;--column aliases--aliases use double quotes, AS can omit select deptno as "no", NAME as "name" from emp;--connectors, connecting columns with columns, columns with characters select deptno|| '--' ||NAME from emp;--String--Date and character can only appear in single quotes select 'hello '|| ename from emp;--delete duplicate rows select distinct deptno from emp;--show table structure desc[ribe] tablename;

II. Filtering and sorting

SELECT *|{[DISTINCT] column|expression [alias], ...} FROM table[WHERE condition(s)];

For example:

--Query specified conditional data select deptno,ename from emp where deptno=10;--String and date enclosed in single quotes--String case sensitive, date format sensitive select ename,job,deptno from emp where ename='King';--Comparison operator (= >

< = !=)select ename,sal from emp where sal1500; 排序 ORDER BY 字段 [DESC|ASC] 例如: select ename,sal from emp order by sal desc;--多列排序--先按第一列排序,如果相同,则按第二列排序,以此类推select * from emp order by sal desc,hiredate desc; 三、单行函数 1、字符函数 --LOWER 转换小写--UPPER 转换大写--INITCAP 首字母大写select lower(ename) from emp;--CONCAT 接接字符串--SUBSTR 截取字符串--LENGTH 字符串长度--INSTR 查找字符串--LPAD 左边填充字符--RPAD 右边填充字符--TRIM([leading|trailing|both] 字符串1 from 字符串2) --TRIM可以删除两边空格,也可删除其他字符--REPLACE 替换字符串select concat('aa','bb') from emp;select substr('abcdefg', 2, 3) from emp;select length('test...') from emp;select instr('hello world', 'w') from emp;select lpad(sal, '10', '0') from emp;select rpad(sal, '10', '*') from emp;select trim(' test ') from emp;--从尾部删除字符串*号select trim(trailing '*' from '**1212121**') from emp;--把字符串中的22替换成88select replace('11223344', '22', '88') from emp; 2、数字函数 --ROUND 四舍五入--TRUNC 截断--MOD 求余select round(25.533,2) from dual;select trunc(25.323,2) from dual;select mod(8, 3) from dual; 3、日期 oracle中日期型数据实际含有两个值:日期和时间。 默认格式为:DD-MON-RR --返回系统时间select sysdate from dual;--两个日期相减,返回日期之间相差的天数select ename,(sysdate-hiredate) / 7 "weeks" from emp;--MONTHS_BETWEEN 两日期相差月数--ADD_MONTHS 指定日期加上若干月数--NEXT_DAY 指定日期的下一个日期--LAST_DAY 本月的最后一天--ROUND 日期四舍五入--TRUNC 日期截断select months_between(sysdate,hiredate) from emp; 4、显式数据类型转换 --TO_CHAR(date, 'format_model')--把日期转换成字符串select to_char(sysdate, 'YYYY MM DD HH:MI:SS') from dual;--TO_CHAR(number, 'format_model')select ename,sal,to_char(sal, '$99,999.00') from emp;--TO_NUMBER(char[,'format_model'])--TO_DATE(char[,'format_model']) 通用函数,适用于任何数据类型,也适用于空值 NVL(expr1,expr2)NVL2(expr1,expr2,expr3)NULLIF(expr1,expr2)COALESCE(expr1,expr2,...) 5、条件表达式 在sql语句中使用if-then-else逻辑 case表达式,sql99语法,类似basic,比较繁锁 decode函数,oracle自已语法,类似java,比较简洁 CASE expr WHEN comparison_expr1 THEN return_expr1 [WHEN comparison_expr2 THEN return_expr2 WHEN comparison_expr3 THEN return_expr3 ELSE else_expr]ENDDECODE(col|expression, search2, result1 [,search3, result2, ..., ] [,defautl])select ename,sal,decode(round(sal/1000),1, '一倍',2, '二倍',3, '三倍','不知倍数') from emp; 6、函数嵌套 单行函数可以嵌套,嵌套函数的执行是由内到外。 四、分组函数 分组函数作用于一组数据,并对一组数据返回一个值。 常用组函数 --AVG--COUNT--MAX--MIN--SUMselect sum(sal) as "total" from emp;select max(sal) from emp;select count(*) from emp where deptno=10;select count(distinct deptno) from emp; 分组数据 通过GROUP BY可以将数据分成若干组 select deptno,avg(sal) from emp group by deptno; (*注意:在select列表中所有未包含在组函数中的列都必须包含在group by中。) --多列分组select deptno,job,avg(sal) from emp group by deptno,job; 过滤分组 通过HAVING子句对分组进行过滤 select deptno,avg(sal) from emp group by deptno having deptno in(10,20); (*注意:不能在where子句中使用组函数,having子句中可以。) 嵌套组函数 select max(avg(sal)) from emp group by deptno; 五、多表查询 oracle的连接 sql99的连接 等值连接 cross joins 不等值连接 natural joins 外连接 using clause 自连接 full or two sided outer joins SELECT table1.column, table2.columnFROM table1,table2WHERE table1.column1=table2.column2;--等值连接select d.dname,e.ename,e.sal from emp e, dept d where e.deptno=d.deptno;--多连接条件和and操作符select d.dname,e.ename,e.sal from emp e, dept d where e.deptno=d.deptno and e.deptno=10; 表的别名 1、使用表别名可简化查询 2、使用表名前缀可以提高执行效率 3、如果使用了表的别名,则不能再使用表的真名。 --不等值连接--查询员工部门名称和工资等级select d.dname, e.ename, e.sal, s.gradefrom emp e, dept d, salgrade swhere e.deptno = d.deptnoand e.sal >

= s.losaland e.sal

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report