※ order by
select [distinct] {column_name…|*|exp|alias_name}
(exp表达式,alias_name别名)
from table_name
order by column_name 排序的标记…
执行顺序:from->select->order by
order by 优先级最低,总是最后执行
※ 排序的标记:
ASC 升序(可省略,默认)
DESC 降序
例:查询员工的名字和薪水,薪水以升序排序
select last_name,salary
from s_emp
order by salary;
select last_name,salary
from s_emp
order by salary asc;
注意:排序的列可以不出现在select后面
select last_name
from s_emp
order by salary desc;
※ order by后面也可以直接跟角标
该角标是select后面排序列的位置,角标从1开始。
select last_name,salary
from s_emp
order by 2;
※ order by 后面可以跟多个排序规则
先按照第一个排,第一个排相等,在按照第二个排序
select id,last_name,salary
from s_emp
order by last_name desc,3 asc;
如果排序的列出现null值,你可以把null当作是无穷大
select id,last_name,commission_pct
from s_emp
order by commission_pct;
注意:排序的时候,升序null值在后面,降序null值在前面,
建议:排序的时候对有null的值排序,可以追加其他排序列
※ 条件限制语句
select [distinct] {column_name…|*|exp|alias_name}
from table_name
while 条件
order by column_name 排序的标记…
执行顺序:from->where->select->order by
注意:from查询的是整张表的内容
where是对from查询的整张表一行一行的筛选,符合的留下,不符合的忽略(效率低,能不写where就不写)
※ 逻辑比较符(比较数值)“= ,< ,> ,<= ,>=”
查询部门编号为41号的部门信息,
select id,name,region_id
from s_dept
where id=41;
查询在41号部门的员工信息
select id,last_name,salary,dept_id
from s_emp
where dept_id=41
查询名字为Patel的用户信息
select id,last_name,salary,dept_id
from s_emp
where last_name=‘Patel’
查询入职时间大于等于1990年1月1日的员工信息
select id,last_name,salary,dept_id
from s_emp
where start_date>=’01-1月-90’;
注意:字符串和时间类型的值用单引号引起来,字符串值表示的时候区分大小写
中文环境的时间格式是dd-Mon-yy
英文环境dd-mm-yy
※ SQL比较符
※ 1.在某个范围之内
between and(闭区间[x,y])
查询员工工资在795到1450中间的员工信息
select id,last_name,salary
from s_emp
where salary between 795 and 1450;
not between and(开区间(x,y))
查询员工工资不在795到1450中间的员工信息
select id,last_name,salary
from s_emp
where salary not between 795 and 1450;
※ 2.在某个范畴
in(list)
查询部门为41或43号部门的员工信息
select id,last_name,salary
from s_emp
where dept_id in(41,43);
不在某一范畴
not in
查询除了41和43好部门的其他所有部门的员工信息
select id,last_name,salary
from s_emp
where dept_id not in(41,43);
※ 3.“和”与“或”
and并且(前后两个条件都要成立)
查询部门在41部门并薪水在900以上的员工信息
select dept_id,last_name,salary
from s_emp
where dept_id=41 and salary>=900;
or 或者(前后两个条件只要成立一个就行)
查询部门为41或43号部门的员工信息
select id,last_name,salary
from s_emp
where dept_id=41 or dept_id=43;
注意:and和or并存的时候
and的优先级高于or
查询部门在41部门并薪水在900以上的员工信息或者43部门的员工信息
select dept_id,last_name,salary
from s_emp
where dept_id=41 and salary>=900 or dept_id=43;
※ 4.不相等
“<> , != , ^=”
查询除了41和43好部门的其他所有部门的员工信息
select id,last_name,salary
from s_emp
where dept_id!=41 and dept_id!=43;
※ like 模糊查询,模糊匹配
_ 表示单个字符的占位
% 表示匹配0到多个字符
查询用户名以P开头的所有用户信息
select id,last_name,salary
from s_emp
where last_name like ’P%’;
查询用户名第二个字母是m的员工信息
select id,last_name,salary
from s_emp
where last_name like ’_m%’;
※ 查询名字以_开头的员工信息
(插入一个_开头的员工)
insert into s_emp(id,last_name)
values(26,’_briup’);
commit;(将缓存中的数据存入数据库)
select id,last_name
from s_emp
where last_name like ’\_%’ escape‘\’;
注:转义可以是任意的符号,必须escape单词限定的都可以作为转义
※ is (not)null
is null(判断为空)
查询没有薪水的员工信息
select id,last_name
from s_emp
where salary is null;
is not null(判断不为空)
查询有薪水的员工信息
select id,last_name
from s_emp
where salary is not null;
查询有提成的员工信息
select id,last_name,commossion_pctZX
from s_emp
where commission_pct is not null;