第4章:运算符

news/2024/10/17 22:29:26/

1.算术运算符 

SELECT 100+10,100-35.5,100*2,100/2,100%30
FROM DUAL;

 ②在sql中“+”没有连接作用,表示加法运算,字符串转换为数值(隐式转换)。非数值看作0处理

SELECT 1001 + '1'
FROM DUAL;

SELECT 100 + 'a'
FROM DUAL;

 

 ③加法运算+NULL 结果是NULL

SELECT 100 + NULL
FROM DUAL;

 

【练习】

查询员工id是偶数的员工信息

select employee_id,last_name,salary
from employees
where employee_id % 2 = 0;

 2.比较运算符

2.1 算术运算符

 2.1.1 等于=

①字符串隐式转换。数值转换为数值,非数值转换为0

select 1 = 2,1 != 2,1 = 1,1 = '1',1 = 'a', 0 = 'a'
from DUAL;

② 字符串跟字符串比较是ANSI编码

select 'a' = 'a','a'='b'
from DUAL;

 ③有NULL参与的结果是NULL

select 1=NULL,NULL=NULL
from DUAL;

 ④查询commission_pct是NULL的结果集

where条件是NULL,结果是NULL。结果集得到结果是1的语句。

select last_name,salary
from employees
where commission_pct = null;

 2.1.2 安全等于<=> 为NULL而生

①没有NULL参与

select 1<=>1,1<=>'1',1<=>'a',0<=>'a'
from dual

②有NULL参与

select 1<=>NULL,NULL<=>NULL
from dual

③查询commission_pct是NULL的结果集

select last_name,salary,commission_pct
from employees
where commission_pct <=> null;

 

 2.2 比较运算符

 2.2.1. is null\is not null\isnull(exp)

①查询commission_pct为null的结果集

select last_name,commission_pct
from employees
where commission_pct is null
或
select last_name,commission_pct
from employees
where  isnull(commission_pct)

 ②查询commission_pct不为null的结果集

select last_name,commission_pct
from employees
where commission_pct is not nul
或
select last_name,commission_pct
from employees
where not commission_pct <=> null

 

【总结】

查询为空的用is null

查询不为空的用 is not null

2.2.2.least() \ greatest

①least()比较两个名字的asci码最小的

select first_name,last_name,least(first_name,last_name)
from employees

 2.2.3.between 条件1 and 条件2

①查询工资在6000到8000的员工信息

select first_name,salary
from employees
where salary between 6000 and 8000;
或
select first_name,salary
from employees
where salary >= 6000 and salary <= 8000;

②查询工资不在6000到8000的员工信息

select first_name,salary
from employees
where not salary between 6000 and 8000;
或
select first_name,salary
from employees
where salary < 6000 or salary > 8000;

 

2.2.4.in(set) \ not in (set)

①查询部门号是10,20,30的员工信息

select first_name,salary,department_id
from employees
where department_id=10 or  department_id=20 or  department_id=30
或
select first_name,salary,department_id
from employees
where department_id IN(10,20,30)

 

 ②查询工资不是6000,7000,8000的员工信息

select first_name,salary
from employees
where salary !=6000 and  salary !=7000 and salary!=8000
或
select first_name,salary
from employees
where salary not in (6000,7000,8000)

 

2.2.5 like :模糊查询

%代表不确定的个数的字符(0个,1个,2个)

_代表不确定的字符

①查询first_name包含’a’员工信息 。

select first_name,salary
from employees
where first_name like '%a%'

 ②查询字符a开头的first_name的员工信息

select first_name,salary
from employees
where first_name like 'a%'

 ③查询first_name包含字符’a’且包含字符’s’的员工信息

select first_name,salary
from employees
where first_name like '%a%s%' or first_name like '%s%a%'
或
select first_name,salary
from employees
where first_name like '%a%' and  first_name like '%s%'

 ④查询第2个字符是’a’的员工信息

select first_name,salary
from employees
where first_name like '_a%'

 ⑤查询第2个字符是‘_‘第3个字符是’a’的员工信息。使用转义字符\

select first_name,salary
from employees
where first_name like '_\_a%'

 3.逻辑运算符,运算结果是1,0,null

 

XOR

a、b两个值不相同,则异或结果为1。如果a、b两个值相同,异或结果为0

【课后练习】

 

1.
select first_name,salary
from employees
where salary not between 5000 and 12000
或
select first_name,salary
from employees
where salary < 5000 or salary > 120002.
select first_name,department_id
from employees
where department_id= 20 or department_id=50
或
select first_name,department_id
from employees
where department_id in (20,50)3.
select first_name,job_id
from employees
where manager_id is null
或
select first_name,job_id
from employees
where manager_id <=> null4.
select first_name,salary,commission_pct
from employees
where commission_pct is not null
或
select first_name,salary,commission_pct
from employees
where not commission_pct  <=> null5.
select first_name
from employees
where first_name like '__a%'6.
select first_name
from employees
where first_name like '%a%' and first_name like '%k%'7.
select first_name
from employees
where first_name like '%e'8.
select first_name,job_id,department_id
from employees
where department_id between 80 and 1009.
select first_name,salary,manager_id
from employees
where manager_id in (100,101,110)


http://www.ppmy.cn/news/51138.html

相关文章

数字化转型危与机,20年老厂的升级之路

“投资大、周期长、见效慢”&#xff0c;是每一家企业在考虑数字化战略时&#xff0c;都会纠结的问题。 打江山容易&#xff0c;守江山难 企业在快速扩张的过程中&#xff0c;往往可以不需要过多的考虑细节的问题&#xff0c;跑马圈地的打法会更加有效。 但是市场占有量开始饱…

Turndown 源码解析:一、辅助函数

extend() Object.assign的补丁。 function extend (destination) {for (var i 1; i < arguments.length; i) {var source arguments[i];for (var key in source) {if (source.hasOwnProperty(key)) destination[key] source[key];}}return destination }repeat() Stri…

基于Java+Springboot+Vue+elememt甜品屋蛋糕商城系统设计和实现

基于JavaSpringbootVueelememt甜品屋蛋糕商城系统设计和实现 博主介绍&#xff1a;5年java开发经验&#xff0c;专注Java开发、定制、远程、指导等,csdn特邀作者、专注于Java技术领域 作者主页 超级帅帅吴 Java项目精品实战案例《500套》 欢迎点赞 收藏 ⭐留言 文末获取源码联系…

(十九)查找算法-线性查找

1 查找算法基本介绍 在 Java 中&#xff0c;常用的查找算法有4种&#xff1a; &#xff08;1&#xff09;顺序&#xff08;线性&#xff09;查找 &#xff08;2&#xff09;二分查找/折半查找 &#xff08;3&#xff09;插值查找 &#xff08;4&#xff09;斐波那契查找 2 线…

《程序员面试金典(第6版)》面试题 16.03. 交点(直线的一般式方程,克莱姆法则,行列式,C++)

题目描述 给定两条线段&#xff08;表示为起点start {X1, Y1}和终点end {X2, Y2}&#xff09;&#xff0c;如果它们有交点&#xff0c;请计算其交点&#xff0c;没有交点则返回空值。 要求浮点型误差不超过10^-6。若有多个交点&#xff08;线段重叠&#xff09;则返回 X 值最…

服务(第十二篇)LVS-DR模式

数据包流向分析&#xff1a; &#xff08;1&#xff09;客户端发送请求到 Director Server&#xff08;负载均衡器&#xff09;&#xff0c;请求的数据报文&#xff08;源 IP 是 CIP,目标 IP 是 VIP&#xff09;到达内核空间。 &#xff08;2&#xff09;Director Server 和 Re…

PCL点云库(3) — common模块

目录 3.1 common模块中的头文件 3.2 common模块中的基本函数 &#xff08;1&#xff09;angle角度转换 &#xff08;2&#xff09;distance距离计算 &#xff08;3&#xff09;random随机数生成 &#xff08;4&#xff09;sping扩展模块 &#xff08;5&#xff09;time获…

同步和异步的区别及优缺点 通俗理解

一、同步和异步的区别 程序里面的同步和异步和我们现实生活理解不太一样&#xff0c;一般我们对同步的理解是同时做很多事情&#xff0c;但程序中的同步是按照任务的顺序执行任务&#xff0c;前一个任务没有执行结束&#xff0c;下一个任务不会执行&#xff0c;要等待上一个任…