转载 http://zy116494718.iteye.com/blog/796827
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 ;
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 ;
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 ;
执行:
call liyukun(@a);
select @a;
7) mysql的group by 语句可以select 没有被分组的字段,如
select id,name,age from A group by age 这样
但是在orcale和sqlserver中是会报错的。这个取出的id,name所在的行是每个分组中的第一行数据。
8)orcale用decode()来转换数据,mysql,sqlserver用case when:
case t.DETECT_RESULT when '2402' then t.SAMPLEID end (必须有end)
9)mysql: 两个select 出的数据相减:
(COUNT(distinct(t.SAMPLEID))-
CONVERT((COUNT(distinct(case t.DETECT_RESULT when '2402' then t.SAMPLEID end))), SIGNED)) AS NEGATIVE
FROM `view_sims_for_report` t
10)convert,cast用法
mysql将varchar转为int
convert(字段名, SIGNED)
字符集转换 : CONVERT(xxx USING gb2312)
类型转换和SQL Server一样,就是类型参数有点不同 : CAST(xxx AS 类型) , CONVERT(xxx,类型)
可用的类型
二进制,同带binary前缀的效果 : BINARY
字符型,可带参数 : CHAR()
日期 : DATE
时间: TIME
日期时间型 : DATETIME
浮点数 : DECIMAL
整数 : SIGNED
无符号整数 : UNSIGNED
11)如果从mysql数据库中取的时候没有乱码,而在Java的List中得到的是乱码的话,那么可能是SQL语句中的有字段不是 varchar的数据类型,这时需要转换convert(字段名, 类型)转换一下,Orcale则用ToChar函数
12)Orcale的大字段用clob,图像用blob,clob字段在Hibernate的映射文件中用String就可以
13) mysql,orcale,sqlserver 语句执行顺序
2作用:把addString添加到a的左边,length 是返回值的长度。
3例子
A : SQL> select lpad('test',8,0) from dual;
LPAD('TEST',8,0)
----------------
0000test
B: select lpad('test',8) from dual;
LPAD('TEST',8)
--------------
test 注:不写最后一个参数,函数会默认在返回值左边加一个空格。
C: SQL> select lpad('test',2,0) from dual;
LPAD('TEST',2,0)
----------------
te
D:SQL> select lpad('test',3) from dual;
LPAD('TEST',3)
--------------
tes
loop
str := to_char(num);
num := num+1;
end loop;
str := to_char(num);
end loop;
num := num+1;
- public int insertSign(final SpaceSign sign) throws Exception {
- try{
- KeyHolder keyHolder = new GeneratedKeyHolder();
- final String sql = "insert into space_sign(userId,userName,nickName,contentText,contentHtml,isPublic,commentCount,userIp,status,insertTime)" +
- " values(?,?,?,?,?,?,?,?,?,?)";
- template.update(new PreparedStatementCreator() {
- public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
- PreparedStatement ps = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
- ps.setInt(1, sign.getUserId());
- ps.setString(2, sign.getUserName());
- ps.setString(3, sign.getNickName());
- ps.setString(4, sign.getContentText());
- ps.setString(5, sign.getContentHtml());
- ps.setInt(6, sign.getIsPublic());
- ps.setInt(7,sign.getCommnetCount());
- ps.setString(8, sign.getUserIp());
- ps.setInt(9, sign.getStatus());
- ps.setTimestamp(10, new java.sql.Timestamp(sign.getInsertTime().getTime()));
- return ps;
- }
- }, keyHolder);
- Long generatedId = keyHolder.getKey().longValue();
- return generatedId.intValue();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- throw new SQLException("发表签名失败", e);
- }
- }
public int insertSign(final SpaceSign sign) throws Exception { try{ KeyHolder keyHolder = new GeneratedKeyHolder(); final String sql = "insert into space_sign(userId,userName,nickName,contentText,contentHtml,isPublic,commentCount,userIp,status,insertTime)" + " values(?,?,?,?,?,?,?,?,?,?)"; template.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS); ps.setInt(1, sign.getUserId()); ps.setString(2, sign.getUserName()); ps.setString(3, sign.getNickName()); ps.setString(4, sign.getContentText()); ps.setString(5, sign.getContentHtml()); ps.setInt(6, sign.getIsPublic()); ps.setInt(7,sign.getCommnetCount()); ps.setString(8, sign.getUserIp()); ps.setInt(9, sign.getStatus()); ps.setTimestamp(10, new java.sql.Timestamp(sign.getInsertTime().getTime())); return ps; } }, keyHolder); Long generatedId = keyHolder.getKey().longValue(); return generatedId.intValue(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new SQLException("发表签名失败", e); } }由于Orcale的ID是在插入该条数据之前就通过select SEQ_BLOG_ID.nextval from dual 获得的,所以直接返回既可。ps:SEQ_BLOG_ID为在数据库中设置的sequence。