本教程中所使用的数据库的建表语句都在“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