问题
I have to write an update query which should work on normal mysql db and h2 database.
This was my original mysql query.
Query1
UPDATE table1 AS mt, table2 AS et
SET mt.country_name = et.country_name,
mt.country_continent = et.country_continent
WHERE mt.id = et.id
But h2 db was complaining syntax error for Query1.
org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "update table1 AS mt,[*]table2 AS et as et mt.country_name = et.country_name, mt.country_continent = et.country_continent WHERE mt.id = et.id ; expected "SET"; SQL statement: update table1 AS mt,table2 AS et as et mt.country_name = et.country_name, mt.country_continent = et.country_continent WHERE mt.id = et.id [42001-199] at org.h2.message.DbException.getJdbcSQLException(DbException.java:451) at org.h2.message.DbException.getJdbcSQLException(DbException.java:427) at org.h2.message.DbException.getSyntaxError(DbException.java:243) at org.h2.command.Parser.getSyntaxError(Parser.java:991) at org.h2.command.Parser.read(Parser.java:4541) at org.h2.command.Parser.parseUpdateSetClause(Parser.java:1228) at org.h2.command.Parser.parseUpdate(Parser.java:1223) at org.h2.command.Parser.parsePrepared(Parser.java:943) at org.h2.command.Parser.parse(Parser.java:788) at org.h2.command.Parser.parse(Parser.java:764) at org.h2.command.Parser.prepareCommand(Parser.java:683) at org.h2.engine.Session.prepareLocal(Session.java:627) at org.h2.engine.Session.prepareCommand(Session.java:565) at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1292) at org.h2.jdbc.JdbcPreparedStatement.(JdbcPreparedStatement.java:77) at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:349) at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:311) at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java)
Then I converted it as follows
Query2
UPDATE table1 AS mt , table2 as et
SET (mt.country_name, mt.country_continent ) =
( SELECT et.country_name, et.country_continent
FROM table2 AS et
WHERE mt.id = et.id )
which I expected to work on both mysql and h2db but this one is throwing syntax error on mysql.
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(mt.country_name, mt.country_continent)= ( SELECT et.country_name, et.country_co' at line 1 0.00073 sec
Can someone one please help me to convert my Query1 to a query which support both on mysql and h2db. I think it should almost look like Query2 but I may be wrong.
来源:https://stackoverflow.com/questions/57200828/convert-mysql-update-query-to-a-query-which-support-on-h2-db