查询条件
- 多字段查询相同值
select * from student where (字段1||字段2||字段n) like '%值%'; - orcale中不存在limit关键字,使用rownum
select * from (select rownum num,s.* from student s) where num > 1;
注意:实际查询语句中,查询字段如果没有指定,直接使用*,会出现ORA-00936:缺失表达式异常,解决方案:需要使用表名.*的方式, - exists语句
exists表示子查询语句返回结果不为空说明where条件成立就会执行主sql语句,如果为空就表示where条件不成立,sql语句就不会执行。not exists和exists相反。(语法举例,实际该场景使用in效果较好)
exists 和 in 使用时机:当比较集合中的数据量小时,使用in(t1 in(t2) t2数据量小且相对固定),in最大限度为1000,超过1000会报异常,处理当时见【根据大量已知id查询数据】;当集合中数据量大时,使用exists(t1 exists(t2) t1数据量小,t2中的数据量非常大)select * from student s where exists(select * from class c where c.name like '高三%' ) temp and temp.id = s.classId;
- 根据大量已知id查询数据
- 使用表达式切分字符,与查询的表做join操作
Select * from student s jion( select regexp_substr(${字符串} , '[^分隔符]+' , 1 , LEVEL) id, LEVEL from dual connect by LEVEL <= regexp_count(${字符串} , '[^分隔符]+') ) temp on s.id = temp.id
- 使用with list语句
WITH id_list AS ( SELECT column_value::NUMBER AS id FROM TABLE(SYS.ODCINUMBERLIST(待查询列表1,2,3……n)) ) SELECT t.* FROM my_table t JOIN id_list ON t.id = id_list.id;
- 使用表达式切分字符,与查询的表做join操作
- 与mybatis集成标签中不可以【;】结尾。以分号结尾会出现命令未正确结束
字段表达式
- 判断表达
case when 条件 then 满足输出 else 不满足输出 end - 字符串查找函数
instr(source,target[,起始位置,几次])查找原字符串中从起始位置(不填默认为0)开始第几次(不填默认为 1.如果为负数系统会报错)出现了目标字符串
查找名称第二个为木的学生:select case when instr(name,'木',1,1) > 0 then '包含' else '不含' end from student;
- to_date日期转换函数
将字符串转为时间,to_date(时间字符串,格式) - trunc截断函数
-- 数字切割 select trunc(111.23) from dual; -- 输出结果111 select trunc(111.23,1) from dual;-- 输出结果111.2 select trunc(111.23,-1) from dual;-- 输出结果110 -- 日期切割 select trunc(to_date('2024-5-10 17:00:00','yyyy-mm-dd hh24:mi:ss'),'yyyy') from dual;-- 输出结果 2024-01-01 trun切割日期时,输出格式分为【yyyy-mm-dd|yyyy-mm-dd hh:mi:ss】:第二个参数表示切割范围(不精确到秒):yy|yyyy表示切割到年份、mm表示到月份、dd表示天、d表示当前日期所在星期的星期一、hh到小时、mi到分