为了实现oracle和mysql都能通用,对oracle数据库语句进行转换。
如下代码
UPDATE btjs_xszffwbt a
SET sys_djztms = '已过期', sys_djzt = 2, sys_spzt = 5, sys_spztms = '已过期'
WHERE sys_spzt = 1
AND xjsxsrq <= '$date$'
AND sys_djzt = 1
AND x_id = (select xxid from btjs_xszffwbt where x_id = '$pkid$');
问题现象:Oracle数据库里无错,但在mysql里面报错
报错提示:[Err] 1093 - You can't specify target table 'a' for update in FROM clause报错排查:最后一句AND x_id = (select xxid from btjs_xszffwbt where x_id = '$pkid$');
Mysql数据库无法识别
报错分析:MySQL是通过临时表来实现FROM子句里面的嵌套查询,那么把嵌套查询装进另外一个嵌套查询里,可使FROM子句查询和保存都是在临时表里进行,然后间接地在外围查询被引用。
解决方式:
方式一:UPDATE btjs_xszffwbt a, btjs_xszffwbt b
SET a.sys_djztms = '已过期', a.sys_djzt = 2, a.sys_spzt = 5, a.sys_spztms = '已过期'
WHERE a.sys_spzt = 1
AND a.xjsxsrq <= '$date$'
AND a.sys_djzt = 1
AND a.x_id = b.xxid AND b.x_id = '$pkid$'
问题:mysql能识别,在oracle里无法识别,报错:missing SET keyword
方式二:UPDATE btjs_xszffwbt a
SET sys_djztms = '已过期', sys_djzt = 2, sys_spzt = 5, sys_spztms = '已过期'
WHERE sys_spzt = 1
AND xjsxsrq <= '$date$'
AND sys_djzt = 1
AND x_id = (select xxid from (
select * from btjs_xszffwbt
) as x where x_id = '$pkid$');
问题:mysql能识别,在oracle里无法识别,报错:missing right parenthesis
正确写法为:
UPDATE btjs_xszffwbt a
SET sys_djztms = '已过期', sys_djzt= 2, sys_spzt= 5, sys_spztms = '已过期'
WHERE sys_spzt = 1
AND xjsxsrq <= '$date$'
AND sys_djzt = 1
AND x_id = (select x_idfrom (
select xxid from btjs_xszffwbt where x_id = '$pkid$') xx)