oracle SQL查询中,如何在where中用条件语句,判断不同情况,追加不同的An...
发布网友
我来回答
共3个回答
懂视网

where子句
--查询部门编号是10的所有的员工
select * from emp where deptno = 10;
--查询部门中工资是3000的员工
select * from emp where sal = 3000;
--找到部门中编号是 7788的员工
select * from emp where empno = 7788;
--查询姓名为SCOTT的员工所有信息
--在使用where 查询条件,字符串是单引号而且区分大小写
select * from emp WHERE ename = ‘SCOTT‘;
--查询所有在日期是1981年5月1日入职的员工信息
--select * from emp where hiredate = ‘1981-5-1‘;
--日期默认格式是 DD-MON-YYYY 查询条件按照日期来,日期也要加单引号
select * from emp where hiredate = ‘1/5月/1981‘;
--查询工资大于3000的员工
select * from emp where sal>=3000; ---注意:sal=>3000 是错误的!数据库将不认识此符号!
--查询工资范围在1500-3000的员工所有信息
select * from emp where sal>=1500 and sal<=3000;
-- between..and...表示介于 两个值之间,包涵边界值
select * from emp where sal between 1500 and 3000;
--查询姓名是KING和SCOTT的详细信息
select * from emp where ename = ‘KING‘ or ename =‘SCOTT‘;
--IN 表示出现在集合中的记录
select * from emp where ename in (‘KING‘,‘SCOTT‘);
--查询工资不等于3000的员工信息
select * from emp where sal <> 3000; --method1
select * from emp where sal !=3000; --method2
select * from emp where not sal = 3000; --method3 取反运算
--查询所有没有提成的员工的信息
-- is null 表示某个字段为空 不为空 is not null
select * from emp where comm is not null;
-- 取反的意思 where not comm is null
select * from emp where not comm is null;
-- not 也可以代表取反的意思
select * from emp where ename not in (‘KING‘,‘SCOTT‘);
--查询所有姓名以s开头的员工信息
-- 使用 like 和 通配符
-- 两个通配符 %代表0个或者多个字符 _一个字符
select * from emp where ename like ‘%S‘; ---字符(或是‘ ‘)里面严格区分大小写。建议全部大写书写语句!
--查询名字中带有0的
select * from emp where ename like ‘%O%‘;
--查询第二个字母是L的员工的所有信息
select * from emp where ename like ‘_L%‘;
--查询员工姓名中带有_的所有的信息
--ESCAPE ‘’ 相当于自己定义一个转义符
select * from emp where ename like ‘%a_%‘ ESCAPE ‘a‘;
Oracle条件查询语句-where
标签:建议 weight dep 查询语句 eth gpo 表示 运算 esc
热心网友
1、先创建一个简单的数据表。
2、插入数据,顺便复习插入数据了,最好多插几个,查询的时候更明显。
3、select后面的字段是等下查询结果所显示的,where就是*的条件,根据where查询到对应的列。
4、如果字段里面有数字的话也是可以根据数字大小进行查询的。
5、加入and语句可以同时*两个条件来查询。
6、用%来查询,百分号在字母前表示以什么字母开头的,这里就是以名字为d开头来查询数据,同理把%放在后面就是以d结尾的名字。
热心网友
你是不是描述的有错误?怎么两次都是username不为空?
估计可以有好几个:
1、可以用union all
select * from table where 1=1 and (username is not null and instr(username , '李四') > 0)
union all
select * from table where username is null
2、你写的这个好像or and的逻辑有问题,可以改改
select * from table where 1=1
and ((username is not null and instr(username , '李四') > 0) or (username is null))
理解错误的话请纠正