oracle语句书写常见问题

1.包含空值的表达式都为空

例:select a,b,a*12,a*12+b from test     若a为null,则a*12和a*12+b为null,若b为null,则a*12+b也为null。

  nvl和nvl2滤空函数,nvl(a,b)判断a是否为Null,如果为null,返回b,否则返回a;nvl2(a,b,c)判断a是否为null,如果为null,返回c,不为null返回b。

2.null永远不=null,判断一个值是否为null。应该用 is null 或者is not null

例子:select * from emp where a=null  执行失败

3.oracle连接符 || 相当于java中对String的相加

例子: select 'Hello'||' World' from dual;

  dual为oracle中内置的伪表。oracle中from后必须跟随一张表名,若select属性不跟任何表相关,则使用from dual;

4.oracle中大小写敏感,mysql中大小写不敏感。

例子:select * from emp where a="King" 和select * from emp where a = "KING" 的结果是不一样的,而在mysql中结果相同。

5.oracle中日期格式敏感。where中日期过滤条件必须符合预定义的日期格式。默认的格式是DD-MON-RR

6.between .. and .. 包含边界,小值在前,大值在后。

7.如果集合中有null值,不能使用not in,可以使用in

例子:select * from emp where deptno not in(10,20,null);查询不出数据

8.模糊查询转义字符的例子

例子:select * from emp where ename like '%\_%' escape '\'

9.oracle的事务是自动开启的,mysql的事务手动开启

10.where语句的解析顺序是从右往左的

例子:where condition1 and condition2;先执行condition2.

  尽量把容易false的语句写在后面,效率更高。

11.order by 后面可以跟列名,表达式,别名,序号

例子:order by sal;order by sal*12;order by 年薪;order by 4;

12.order by 排序时,如果排序的列有null值,默认null显示在后面,oracle中null值最大

例子:order by conn desc nulls last;

  如果不加nulls last,conn为null的记录为显示在最上方,加上nulls last,含有null值的记录便会出现在最下方

猜你喜欢

转载自www.cnblogs.com/Begodpath/p/9313920.html