SELECT查询


Select查询 ##1.最简单的查询方式 select * from emp; select * from dept; select empno,ename,sal from emp; ## 2.使用算术表达It select empno, ename,sal,sal1.08 from emp; select empno, ename,sal, sal12 from emp; select empno, ename,sal, sal12+1000 from emp;>注:在Select语句中,对数值型数据可以使用算术运算符创建表达式。 ## 3.使用字段别名 select empno as 员工编号,ename 员工姓名, sal12 年薪 from emp; select empno, ename "Ename", sal12 "Anual Salary" from emp; select sal12+5000as“年度工资(加年终奖)"from emp;### 字段别名·重命名查询结果中的字段,以增强可读性·别名如果含有空格或其他特殊字符或大小写敏感,需用双引号引起来。-AS可以省略 ## 4.DISTINCT关键 >缺省情况下,查询结果中包含所有符合条件的记录行,包括重复行select deptno from emp; >使用DISTINCT关键字可从查询结果中清除重复行 select distinct deptno from emp: select distinct iokfrom emp;>DISTINCT的作用范围是后面所有字段的组合 select distinct deptnojob from emp; ## 5.排序>排序方式包括升序(asc,缺省)和降序(desc)两种 ### 5.1按单字段排序 select empno, ename, sal from emp order by sal; select empno,ename,sal from emp order by sal desc ; #t 5.2按多字段排序select deptno, empno, ename, sal from emp order by deptno, sal; ##t 5.3使用字段别名排序 select empno, ename, sal12 annsal from emp order by annsal; ### 5.4查询条件中使用逻辑运算符 selectfromemp where deptno = 10 and sal > 1000; select * from emp where deptno = 10 or job = 'CLERK’ ; select * from emp where sanot in(800,1500,2000);## 6.SQL优化问题1.AND:把检索结果较少的条件放到后面 2.OR:把检索结果较多的条件放到后面 ## 7.四种运算符> 共计四种运算符:算术>连接>比较>逻辑 优先级|运算符 ----1|*、/2|+、-3|=、>、>=、<、<=、<>4|is[not]null, like、[notin 5 notlbetween.and 6 not 7and 8 or ###可使用小括号强行改变运县测子 select " trom emp wherejob='SALESMAN' orjob='CLERK" and sal>=1280; select * from emp where (job='SALESMAN' or jo!-.") and sal>=1280