select查询
DQL主要用于数据的查询,其基本结构是使用select子句。
对emp表查询:
select * from emp;
运行结果:

显示部分列:
select empno,ename,sal from emp;
运行结果:

显示部分行 where:
select empno,ename,job,mgr from emp where sal>2000;
运行结果:

起别名:
-- 起别名(把as,单双引号省略了)
select empno 员工编号, ename 员工姓名, sal 公资 from emp;
-- as alias别名
select empno as 员工编号 , ename as 员工姓名, sal as 公资 from emp;
-- 单双引号(在别名中有特殊符号的时候,单双引号,不能省略)
select empno as '员工编号' , ename as "员工姓名", sal as '公资' from emp;
运行结果(三种都一样):

算术运算符:
select empno,ename,sal,sal+1000 as '涨薪后',deptno from emp where sal<2500;
运行结果:

去重操作:
select job from emp;
运行结果:

select distinct job from emp;
运行结果:

select job,deptno from emp;
运行结果:

select distinct job,deptno from emp;
对后面的所有组合去重,而不是单独的某一列去重

排序:
select * from emp order by sal; -- 工资排序 默认情况下按照升序

select * from emp order by sal asc; -- 升序可默认不写

select * from emp order by sal desc; -- 降序

select * from emp order by sal asc,deptno desc; -- 在工资升序的情况下,deptno按照降序排列

浙公网安备 33010602011771号