喝咖啡的企鹅

先做事情,再看心情……
posts - 16, comments - 0, trackbacks - 0, articles - 0

Oracle表查询

Posted on 2009-11-28 16:59 咖啡企鹅 阅读(154) 评论(0)  编辑  收藏 所属分类: SQL

查询时过滤重复行DISTINCT关键字
如:
SELECT DISTINCT deptno,job FROM emp;

NULL运算处理:
NVL判断
如:
SELECT ename,(sal+comm)*12 "annsal" FROM emp;--当comm为null时整个算式为空
SELECT ename,(sal+NVL(comm,0))*12 "annsal" FROM emp;--当comm为null时替换为0

LIKE匹配:
%任意个数字符;-单个字符
如:
SELECT * FROM emp WHERE ename LIKE 'S%';
SELECT * FROM emp WHERE ename LIKE 'S__T_';

排序ORDER BY 和 ASC 与 DESC
ASC与升序,可省缺;DESC降序
如:
SELECT * FROM emp ORDER BY sal ASC;
SELECT * FROM emp ORDER BY sal DESC;
--C先按照deptno升序,同deptno里按sal降序
SELECT * FROM emp ORDER BY deptno ASC,sal DESC;

排序GROUP BY 与 HAVING
GROUP BY分组排列,与ORDER BY一样,其后跟字段次序,也就是分组优先次序; 
SELECT AVG(sal),MAX(sal),deptno,job FROM emp GROUP BY deptno,job;
HAVING与GROUP BY联合使用,HAVING 后跟条件,下面两句效果是等同的:
SELECT * FROM (SELECT AVG(sal) asal,deptno FROM emp GROUP BY deptno) A WHERE A.asal>2000;
SELECT AVG(sal),DEPTNO FROM emp GROUP BY deptno HAVING AVG(sal)>2000;

注:组函数只能出现在选择列表、HAVING、ORDER BY子句中;
SELECT语句中出现以下三者,其出现顺序限定为GROUP BY、HAVING、ORDER BY;


只有注册用户登录后才能发表评论。


网站导航: