I'm trying to do an update, saving the old value if the new one is NULL, SQL query is
INSERT INTO scheme (id,type)
VALUES (1,1) ON DUPLICATE KEY
UPDATE scheme
SET type=COALESCE(1,type)
WHERE id=1
the error text is
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'SET type=COALESCE(1,type)
WHERE id=1' at line 4
at sun.reflect开发者_如何学Python.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
I guess that it is just something obvious I'm missing, but I can not figure it out :(
You never need a WHERE
in a ON DUPLICATE KEY
query (and it should always break in MySQL) -- the conflicting line has already been found and since there cannot be two of them, you have to update that one. The WHERE
is implied.
Try this instead:
INSERT INTO scheme (id,type)
VALUES (1,1) ON DUPLICATE KEY
UPDATE scheme
SET type=COALESCE(1,type)
精彩评论