DECODE(value, if1, val1, if2,val2,...,ifn, valn, val):如果value等于if1,则返回val1,如果value等于if2,则返回value2...如果value等于ifn,则返回valn,否则返回val;MySQL对于这种判断可以通过case when then else end;l来判断,即:case when value=if1 then val1 when value=if2 then val2,,,when value=ifn then valn else val end;
-- rownum语法如下: SELECT * FROM XJ_STUDENT WHERE ROWNUM = 1; -- 查询第一条数据 SELECT * FROM XJ_STUDENT WHERE ROWNUM <= 10; -- 获取前10条数据 -- 但rownum不支持查询后几条或第n(n>1)条数据,例如以下sql是不支持的 SELECT * FROM XJ_STUDENT WHERE ROWNUM > 2; SELECT * FROM XJ_STUDENT WHERE ROWNUM = 3;
-- limit 语法如下: SELECT * from fw_department limit 3; -- 查询前3条数据 SELECT * from fw_department limit 2, 4; -- 从第2(序号从0开始)条开始,查4条记录 4. 空数据排序(nulls first 和nulls last)
-- null值排在最前 SELECT * FROM FW_DEPARTMENT A ORDER BY A.REMARK DESC NULLS FIRST -- null值排在最后 SELECT * FROM FW_DEPARTMENT A ORDER BY A.REMARK DESC NULLS LAST
-- MySQL 可通过IF和ISNULL函数达到相同的效果 -- null值排在最后 select * from FW_DEPARTMENT A order by IF(ISNULL(A.REMARK),1,0),A.REMARK desc -- null值排在最前 select * from FW_DEPARTMENT A order by IF(ISNULL(A.REMARK),0,1),A.REMARK desc 5. 表(左/右)关联(+)
-- Oracle 左关联 select * from taba, tabb where taba.id = tabb.id(+); -- Oracle 右关联 select * from taba, tabb where taba.id(+) = tabb.id; -- MySQL 左关联 select * from taba left join tabb on taba.id=tabb.id; -- MySQL 右关联 select * from taba right join tabb on taba.id=tabb.id; 6. 删除语法
-- Oracle 可执行,但MySQL中不能执行 DELETE FROM FW_DEPARTMENT A WHERE A.DEPID = '111'; DELETE FW_DEPARTMENT WHERE DEPID = '111'; -- MySQL中删除语句格式如下: DELETE FROM FW_DEPARTMENT WHERE DEPID = '111'; 7. 递归查询(start with connect by prior)
MySQL不支持(start with connect by prior)的这种递归查询,但可以通过自定义函数来实现。
-- Oracle 递归查询 查询部门ID为‘1111’的所有子部门(包含自身) SELECT * FROM FW_DEPARTMENT START WITH DEPID='1111' CONNECT BY PRIOR DEPID = PARENTDEPID; -- Oracle 递归查询 查询部门ID为‘1111’的所有父部门(包含自身) SELECT * FROM FW_DEPARTMENT START WITH DEPID='1111' CONNECT BY PRIOR PARENTDEPID = DEPID;
-- MySQL 先创建fun_getDepIDList函数,用于查询部门ID字符串 CREATE FUNCTION fun_getDepIDList(rootId VARCHAR(32)) RETURNS VARCHAR(6000) BEGIN DECLARE pTemp VARCHAR(6000); DECLARE cTemp VARCHAR(6000); SET pTemp='$'; SET cTemp=rootId; WHILE cTemp is not null DO set pTemp=CONCAT(pTemp,',',cTemp); SELECT GROUP_CONCAT(depid) INTO cTemp from fw_department WHERE FIND_IN_SET(PARENTDEPID,cTemp)>0; END WHILE; RETURN pTemp; END;
-- 查询部门ID为‘1111’的所有子部门(包含自己) select * from fw_department where FIND_IN_SET(DEPID, fun_getDepIDList('1111'));
-- 查询部门ID为‘1111’的所有父部门(包含自己) select * from fw_department where FIND_IN_SET('1111', fun_getDepIDList(DEPID)); 8. merge into
MySQL不支持(merge into),但提供的replace into 和on duplicate key update可实现相似的功能。
-- Oracle merge into (有则修改,无则新增) MERGE INTO TMPDEPTAB A USING (SELECT '1111' DEPID, '哈哈' DEPNAME FROM DUAL) B ON (A.DEPID = B.DEPID) WHEN MATCHED THEN UPDATE SET A.DEPNAME = B.DEPNAME WHEN NOT MATCHED THEN INSERT(DEPID, DEPNAME) VALUES(B.DEPID, B.DEPNAME);
-- MySQL replace into (特点:1、先删后增; 2、插入/更新的表必须有主键或唯一索引; -- 3、未修改/新增的数据项,如果必填,则必须有默认值) -- 1、由于是先删后增,所以需要满足以下2个条件之一: -- 1.要么必填项有默认值; -- 2.要么插入/更新时为没有默认值的必填项赋值, 否则新增时会报错。 -- 2、表中需要有主键或唯一索引,否则下面语句如果执行多次,表中会出现重复数据。 replace into fw_department(DEPID,PARENTDEPID,DEPNO,DEPNAME) values('1111111', '1234','123', '哈哈');
-- MySQL on duplicate key update (特点:1、插入/更新的表必须有主键或唯一索引; -- 2、未修改/新增的数据项,如果必填,则必须有默认值) insert into fw_department(depid,parentdepid,depno,depname) select '1111111' depid, '123' parentdepid, 'e12' depno, '哈哈哈哈' depname from fw_department on duplicate key update parentdepid = values(parentdepid), depno=values(depno), depname=values(depname); 9. with