过滤和排序数据

前言

本片文章为过滤和排序

一、过滤

1.过滤行

语句格式

select ... from table
where condition(s);//condtion(s)是过滤条件

作为过滤条件的列可以不是需要查询的列,但是需要存在于table中
where紧挨着from,有筛选条件时就需要用到where

-例子

select employee_id,last_name from employees
where salary>5000;//查询工资大于5000的员工的empolyee_id和last_name

2.字符和日期

字符和日期要包含在单引号
字符大小写敏感,日期格式敏感

  • 例子1
select employee_id,last_name,salary from employees
where last_name='Higgins';//查询名为Higgin的员工的empolyee_id、last_name和salary

sql语句不区分大小写,但是作为字符串出现时需要区分大小写,如果大成higgins就会出错(在字符串、字符和日期函数中要注意)

  • 例子2
select last_name,hire_date from employees
where hire_date='7-6月-1994';//查询hire_date为1994年6月7日的员工的last_name和hire_date

默认的日期格式是DD-MON月-RR

select last_name,hire_date from employees
where to_char(hire_date,'yyyy-mm-dd')='1994-06-07';

还可以写成以上形式。to_char是强制转换类型符号,把hire_date转换为字符串型,其格式为yyyy-mm-dd;筛选出转换后为1994-06-07的员工

3.比较运算

操作符含义
=等于(不是==
>大于
>=大于等于
<小于
<=小于等于
<>不等于(也可以是!=
:=赋值

4.其他比较运算

操作符含义
between…and…在两个值之间(包含边界
in(set)等于值列表中的一个
like模糊查询
is null空值
  • 例子1
select last_name,hire_date,salary from employees
where salary>=4000 and salary<=7000;//查询工资在4000-7000之间(包括4000和7000)员工的last_name、hire_date和salary

等价于

select last_name,hire_date,salary from employees
where salary between 4000 and 7000;
  • 例子2
select last_name,department_id,salary from employees
where department_id=90 or department_id=80 or department_id=70;//查询department_id是70、80、90的员工的last_name、department_id和salary

等价于

select last_name,department_id,salary from employees
where department_id in(70,80,90);
  • 例子3
select last_name,department_id,salary from employees
where last_name like '%a%';//查询名字中含有a的员工的last_name、department_id和salary
select last_name,department_id,salary from employees
where last_name like '_a%';//查询名字中第二个字母是a的员工的last_name、department_id和salary

一个下划线表示一个字符

select last_name,department_id,salary from employees
where last_name like '__a%';//查询名字中第三个字母是a的员工的last_name、department_id和salary

两个下划线之间不能有空格

select last_name,department_id,salary from employees
where last_name like '%\_%' escape '\';//查询名字中有下划线的员工的last_name、department_id和salary

\是一个转义字符,让下划线不再表示为字符,而是普通的下划线

  • 例子4
select last_name,department_id,salary,commission_pct from employees
where commission_pct is null;//查询commission_pct是空值的员工的last_name、department_id、salary和commission_pct

5.逻辑运算

操作符含义
and逻辑并
or逻辑或
not逻辑否
  • 例子
select last_name,department_id,salary from employees
where department_id=80 and salary<=5000;//查询department_id是80而且salary小于等于5000的员工的last_name、department_id和salary

二、排序

使用order by子句排序,且order by子句在select语句的结尾
asc(ascend):升序 desc(descend):降序
order by默认升序
语句格式

select...from table
order by...(asc/desc)
  • 例子
select last_name,department_id,salary from employees
order by salary asc,last_name asc;//先将salary升序排列,当salary相同时,再按照last_name升序排列

多层排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值