首页 > 数据库 > MySQL > 正文

MySQL与Oracle的语法区别详细对比

2024-07-24 12:48:44
字体:
来源:转载
供稿:网友
Oracle和mysql的一些简单命令对比
1) SQL> select to_char(sysdate,'yyyy-mm-dd') from dual;
  SQL> select to_char(sysdate,'hh24-mi-ss') from dual;
  mysql> select date_format(now(),'%Y-%m-%d');
  mysql> select time_format(now(),'%H-%i-%S');
  日期函数
  增加一个月:
  SQL> select to_char(add_months(to_date ('20000101','yyyymmdd'),1),'yyyy-mm-dd') from dual;
  结果:2000-02-01
  SQL> select to_char(add_months(to_date('20000101','yyyymmdd'),5),'yyyy-mm-dd') from dual;
  结果:2000-06-01
  mysql> select date_add('2000-01-01',interval 1 month);
  结果:2000-02-01
  mysql> select date_add('2000-01-01',interval 5 month);
  结果:2000-06-01
  截取字符串:
  SQL> select substr('abcdefg',1,5) from dual;
  SQL> select substrb('abcdefg',1,5) from dual;
  结果:abcdemysql> select substring('abcdefg',2,3);
  结果:bcd
  mysql> select mid('abcdefg',2,3);
  结果:bcd
  mysql> select substring('abcdefg',2);
  结果:bcdefg
  mysql> select substring('abcdefg' from 2);
  结果:bcdefg
2) 在MySQL中from 后的表如果是(select.......)这种,那么后面必须有别名
3) 连接字符串在Oracle中用|| ,SqlServer中用+,MySQL中用concat('a','b','c')

4)
在SqlServer中的写法:
复制代码 代码如下:

declare @id varchar(50);
set @id='4028e4962c3df257012c3df3b4850001';
select * from sims_sample_detect where ID= @id;

在MySQL中的写法:
复制代码 代码如下:

set @a = 189;
select * from bc_article where id = @a //不用declare

在Orcale中的写法:

5)MySQL存储过程:
复制代码 代码如下:

DELIMITER $$
DROP PROCEDURE IF EXISTS `SIMS`.`transaction_delSampleInfo`$$
CREATE DEFINER=`root`@`%` PROCEDURE `transaction_delSampleInfo`(in sampleInfoId varchar(50))
BEGIN
start transaction;
update sims_sample_info set del='1' where ID = sampleInfoId;
update sims_sample_detect set del='1' where SAMPLE_ID_PARENT = sampleInfoId;
update sims_sample_detect_info set del='1' where DETECT_ID in(
select ID from sims_sample_detect where SAMPLE_ID_PARENT = sampleInfoId
);
commit;
END$$
DELIMITER ;

变量名不能跟列名相同,否则效果为1=1,且MySQL不区分大小写。

6)mysql 游标
mysql没有像orcale的动态游标,只有显示游标,例子如下:
复制代码 代码如下:

DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`liyukun`$$
CREATE DEFINER=`ids`@`localhost` PROCEDURE `liyukun`(out z int)
BEGIN
declare count1 int;
DECLARE done INT DEFAULT 0;
declare v_haoma varchar(50);
declare v_yingyeting varchar(100);
DECLARE cur1 CURSOR FOR select haoma,yingyeting from eryue where id<2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
//这里和oracle有区别,Oracle的PL/SQL的指针有个隐性变
量%notfound,Mysql是通过一个Error handler的声明来进行判断的
OPEN cur1;
cur1: LOOP
FETCH cur1 INTO v_haoma,v_yingyeting;
IF done=1 THEN //如果没有数据了,则离开
LEAVE cur1;
ELSE
select count(*) into count1 from year2012 where haoma=v_haoma ;
if(count1=0) then
insert into year2012(haoma, yingyeting)
values(v_haoma,v_yingyeting);
else
set z = z+1;
update year2012 set eryue = ‘100' where haoma=v_haoma;
end if;
END IF;
END LOOP cur1;
CLOSE cur1;
END$$
DELIMITER ;
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表