SQL教程——where语法

本教程中所使用的数据库的建表语句都在“SQL教程——索引”这篇文章中,点击链接直达:索引&建表语句

摘要:本文主要SQL语句中where的语法和使用

 

where

 

    (1)、按条件表达式筛选

    条件运算符:>  <  =  !=  <>  >=  <=

    (2)、按逻辑表达式筛选

    逻辑运算符:    &&        ||        and        or        not

    (3)、模糊查询

    like        between and        in        is null


(1)

select * from employees where salary > 12000;

select last_name, department_id from employees where department_id != 90;

(2)

select last_name, salary, commission_pct from employees where salary >= 10000 and salary <= 20000;

#查询部门编号不是在90和110之间,或者工资高于15000的员工信息。

select * from employees where not (department_id >= 90 and department_id <= 110) or salary > 15000;

 (3)

select * from employees where last_name like '%a%';            #%代表任意多个字符

#查询员工名中第三个字符为e, 第五个字符为a的员工名和工资
select last_name, salary from employees where last_name like '__e_a%';         #“_”代表任意一个字符

#查询员工名中第二个字符为_的员工名
select last_name from employees where last_name like '_/_%';                #“/”代表一个转义字符

#between and 包含边界值
select * from employees where employee_id between 100 and 200;
         
#查询员工编号为 IT_DPOG、AD_VP、 AD_PRES中的一个员工名和工种编号————or实现
select last_name,job_id from employees where job_id = 'IT_PROT' or job_id = 'AD_VP' or JOB_ID = 'AD_PRES';
         
#查询员工编号为 IT_DPOG、AD_VP、 AD_PRES中的一个员工名和工种编号————in实现
select last_name,job_id from employees where job_id in ('IT_PROT',  'AD_VP',  'AD_PRES');        #注意此处不支持统配符,相当于“=”,只有like是支持通配符的。

#查询没有奖金的员工名和奖金率
select last_name, commission_pct from employees where commission_pct = null; #这样不可以,因为“=”不能判断null值,只能用“is”,还有is not null,注意is只是与null搭配的!!!不能is not 后边跟字符串和数值

#安全等与<=>可以同时替代=和is,也即是说它既可以判断null值也可以判断数值
select last_name, commission_pct from employees where commission_pct <=> nul

 

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

请保持优秀。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值