SQL中的SELECT_简单查询语句总计

–scott用户不可以接纳,使用system登录
–修改用户scott账户解锁
ALTER USER SCOTT ACCOUNT UNLOCK;
–重新安装密码 (identified 被识其它)
alter user scott identified by tiger;

–选取 所有字段 scott用户单位表
SELECT * FROM scott.dept;
–员工表
SELECT * FROM scott.emp;

–SELECT{*, column [alias],…}FROM table;
SELECT empno,ename,sal FROM scott.emp;

–SELECT语句中的算术表明式
SELECT empno,ename,sal,sal * 12 FROM scott.emp;

–运算符的先行级
SELECT empno,ename,sal,sal * 12 + 100 FROM scott.emp;
SELECT empno,ename,sal,sal * (12 + 100) FROM scott.emp;

–字符串的连接操作符 \\(相当于java的++)
–把五个字段的数额运用_连接起来
SELECT empno ||’_’|| ename FROM scott.emp;

/**
重点:NULL 空值
空值是指不可用,未分配的值
空值不等于零或空格
肆意档次都得以支撑空值
概括空值的此外算术表明式都等于空
*/
–查询scott用户的emp表的装有列
SELECT * FROM scott.emp;
–查询scott用户的emp表的拥有列,条件为comm等于0
SELECT * FROM scott.emp WHERE comm = 0;
–查询scott用户的emp表的具有列,条件为comm等于空字符串
SELECT * FROM scott.emp WHERE comm = ”;
–查询scott用户的emp表的富有列,条件为comm等于空
SELECT * FROM scott.emp WHERE comm = NULL;
SELECT * FROM scott.emp WHERE comm IS NULL;
SELECT * FROM scott.emp WHERE comm IS NOT NULL;
SELECT empno,ename,sal,comm,(sal + comm,sal) * 12 FROM scott.emp;

–nvl函数(Java的法子,传参数进去再次回到结果)
–nvl(首个参数,第二个参数),倘若第一个参数为null,则取第二个参数
SELECT empno,ename,sal,comm,NVL(sal + comm,sal) * 12 FROM scott.emp;

–定义字段的别名,注意,别名不帮忙使用单引号
SELECT empno,ename,sal,sal * 12 AS yearsal FROM scott.emp;
SELECT empno,ename,sal,sal * 12 yearsal FROM scott.emp;
SELECT empno,ename,sal,sal * 12 AS “yearsal” FROM scott.emp;
SELECT empno,ename,sal,sal * 12 “yearsal” FROM scott.emp;
–查询规定:empno叫做columnName列名,eID叫做columnLable列标签(自己定义的别名)
SELECT empno AS eID,ename,sal,sal * 12 AS yearsal FROM scott.emp;
–JDBC中 getInt(String columnLable)
如若有别名则是别名,假使没有别名则columnLable就是别名

–DISTINCT关键字彰着的有分此外
–缺省情形下询问呈现所有行,包括重复行
SELECT deptno FROM scott.emp;
–DISTINCT关键字去除重复数据
SELECT DISTINCT deptno FROM scott.emp;
–DISTINCT的职能范围是末端所有字段的构成
SELECT DISTINCT deptno,ename FROM scott.emp;
SELECT * FROM scott.emp;
SELECT DISTINCT deptno,job FROM scott.emp;
–为何DISTINCT的功力范围是末端所有字段的咬合
SELECT DISTINCT deptno,ename FROM scott.emp WHERE deptno = 30;
–DISTINCT
deptno之后比如30只有一条记下,而30有6个ename,所以不可能展现完整的数量

–where子句 限制筛选数据,必须跟在from之后
SELECT * FROM scott.emp WHERE deptno = 20 OR deptno = 30;
SELECT * FROM scott.emp WHERE deptno = 30 AND mgr = 7698;

–相比较运算符
SELECT * FROM scott.emp WHERE sal >= 800 AND sal <= 1600;
–between and
SELECT * FROM scott.emp WHERE sal BETWEEN 800 AND 1600;
–in 包含,in执行的时候会拆分成一堆的or
SELECT *Java, FROM scott.emp WHERE deptno = 20 OR deptno = 30;
SELECT * FROM scott.emp WHERE deptno IN(20,30);
–like模糊查询 区分轻重缓急写
–%匹配所有 _匹配一个字符
–查询所有以 “s” 先导的员工
SELECT * FROM scott.emp WHERE ename LIKE ‘S%’;
–查询所有以”s”结尾的员工
SELECT * FROM scott.emp WHERE ename LIKE ‘%S’;
–查询名字中含有”S”的员工
SELECT * FROM scott.emp WHERE ename LIKE ‘%S%’;
–查询名字中第二个字符是A的员工
SELECT * FROM scott.emp WHERE ename LIKE ‘_A%’;

–优先级的规则 先and 再or
SELECT ename,job,sal
FROM scott.emp
WHERE job=’PERSIDENT’
OR job=’SALESMAN’
AND sal>1500;

SELECT ename,job,sal
FROM scott.emp
WHERE job=’SALESMAN’
OR (job=’PERSIDENT’
AND sal>1500);

–order by 以…排序
–desc descend 降序
–asc ascend 升序
–对结果集排序 order by asc(升序 默认) desc(降序)
–注意:order by只可以出现在sql语句的尾声一行
–遵照薪水从低到高排序
SELECT * FROM scott.emp ORDER BY sal;
SELECT * FROM scott.emp ORDER BY sal asc;
SELECT * FROM scott.emp ORDER BY sal desc;

相关文章