天天看点

Oracle 与 MySQL 的差异分析(4):SQL写法

Oracle 与 MySQL 的差异分析(4):SQL写法

1 常量查询

1.1 Oracle

select 7*8from dual;

1.2 MySQL

MySQL 中没有DUAL表,查询一个常量时可以不用from一个表。

2 TOP N和分页查询

2.1 Oracle

select *from table where rownum <= 100;

2.2 MySQL

select *from table limit 0,100;

MySQL 中没有rownum和rowid这两个伪列。

3 引号

MySQL 中字符串既可以用单引号也可以用双引号,而 Oracle 中只能用单引号。

insert intot_test1(abc) values(“2015”);

4 NULL和空字符串

4.1 Oracle

对于字符类型字段,null 和“空字符串”是等价的,都要用is null 或 is not null 来判断。

4.2 MySQL

null 和“空字符串”是不等价的,null 表示什么都没有,而“空字符串”则表示值是存在的,只不过是个空值。判断 null 和 Oracle 一样,用 is null 或 is not null,而对空字符串的判断却可以用“=”判断。

5 外连接

5.1 Oracle

这两种写法都是可以的:

select *from t_test2 a left outer join t_test3 b on a.name=b.name;

select *from t_test2 a, t_test3 b where a.name=b.name(+);

5.2 MySQL

不支持这种写法:

select *from t_test2 a, t_test3 b where a.name=b.name(+);

6 insert多条数据

6.1 Oracle

不支持。

6.2 MySQL

可以这样 insert 多条数据:

insert intot_test4 values(“11”),(“12”),(“13”);

7 组函数

MySQL 中组函数在 select 语句中可以随意使用,但在 Oracle 中如果查询语句中有组函数,那其他列名必须是组函数处理过的,或者是group by子句中的列,否则报错。如:

selectname, count(money) from user;

这个放在 MySQL 中没有问题,而在 Oracle 中就有问题了。

8 添加表字段时指定位置

8.1 Oracle

不支持,新增列都在最后。

8.2 MySQL

可以指定新增列在某个列后面:

alter tablet_test5 add ddd int after abc;

9 关联更新

A 和B 表连接,对于关联的数据,用A 的某个列的值更新 B 的某个列。

9.1 Oracle

不支持 update 多个表,update 语句也不支持连接。

9.2 MySQL

MySQL 支持这种写法,可以 update 多个表并直接连接。

updatet_test6 a, t_test7 b set b.name = a.name where b.id = a.id

10 select 嵌套查询必须有别名

10.1 Oracle

内部查询可以没有别名:

select *from (select * from t_test8);

10.2 MySQL

内部查询必须有别名,否则会报错:

select *from (select * from t_test8) a;

11 全外连接

11.1 Oracle

支持:

select *from t_userinfo a full outer joint_userserviceinfo b on a.phonenumber = b.phonenumber;

11.2 MySQL

不支持。