登陆 username:scott password:tiger sqlplus/sqlplusw/
desc dept;查看dept的表结构(字段)
查询语句
select * from dept;
select deptno,dname,loc from dept;
select empno.sal.comm,sal+comm from emp;(运算)
select empno,sal,sal+100 from emp;(运算)
select sysdate from emp;(系统时间)
select sysdate from dual;(dual是虚表,这样只显示了时间)
select sysdate+1 from dual;(时间的运算)
select ename,sal*12 from emp;
select empno,(sal-100)*14 from emp;
select deptno||dname from dept;(字符串连接)
select deptno||'部门标号' from dept;(类型都转换成最高级的再运算)
select deptno||001 from dept;(数值型的连接)
空值的任何算数表达式都等于空
select empno,sal,comm,sal+comm from emp;
selec ename||null from emp;
selec ename||'' from emp;
select lsat_name||null||first_name from employees;(空值连接运算)
select ename "雇员 姓名" from emp;(字段别名改变列标题)
select ename "Ename" from emp;(字段别名强制大小写)
文本字符串
insert into dept valuse(50,'develop','China');
重复记录
select distinct danme from dept;(使用destinct关键字,要放在前面)
select distinct danem,deptno from dept;(两列条件同时满足)
条件查询
select empno,ename,deptno from emp
where deptno='10';(where关键字)
select empno,ename,deptno from emp
where deptno=10 and ename='clark'(and与)
select empno,ename,deptno from emp
where deptno=10 or deptno=20;(or或)
select empno,ename,deptno from emp
where deptno!=10;(不等于)
select empno,ename,hiredate from emp
where hiredate='03-4月-89'(注意日期的格式)
查看当前的默认日期格式
select * from v$nls_parameters;(所有格式)
或者 select sysdate from dual;(只有日期格式)
select empno,deptno from emp
where deptno<>10;(不等于)
select depno,hiredate from emp
where hiredate > '01-01月-82';(越接近现在的日期越大)
其他比较运算符号:
(BETWEEN)
select ename,sal from emp
where sal>=1000 and sal<=2000;
select empno,sal from emp
where sal between 1000 and 2000;
(IN)
select empno,deptno from emp
where deptno=10 or deptno=20;
select empno,deptno from emp
where deptno in(10,20);
(LIKE)
select empno,ename from emp
where ename like 'A%';(LIKE)
select empno,ename from emp
where ename like '_A%';(LIKE关键字)
(SECAPE关键字)
update dept set dname='develop_' where deptno=60;
select * from dept
where dname like '%\_' escape '\';(\是转义符号)
select * from dept
where dname like '%$_' escape '$'($是转义符号)
select empno,comm from emp
where comm is null;(NULL关键字)
select ename,comm from emp
where comm is not null;(关键字)
select empno,comm from emp
where comm='';(Null关键字)
(优先级)
select deptno,dname from dept
where deptno=10 and dname like'a%' or loc like 'J%';
select deptno,dname from dept
where deptno=10 or dname like'a%' and loc like 'J%';
对查询数据排序:
(ORDER BY子句默认升序ASC,降序关键字是DESC)
select * from emp
order by empno,sal desc;
select empno,sal*12 from emp
order by 1 asc;(1代表的是按哪个字段排序)
slect * from emp
order by ename;(字符串的按首指母排序,hiredte desc离现在最近的)
使用列的别名排序
select * from emp
order by deptno,sal(先按deptno排,然后按sal排)