我有两张名为'Trending'和'Questbank'的表。
我试图插入趋势表并同时更新questbank表。以下是我的代码。
PreparedStatement stmt = connection.prepareStatement(
"INSERT INTO TRENDING ( TOPIC , CATEGORY , QUESTION , ANSWER , PROFILEPIC , TICK , URL ) VALUES( ?, ? , ? , ? , ? , now() , ? ) Returning ID AND UPDATE QUESTBANK SET wasanswered = 'yes' WHERE id = ?");
stmt.setString(1, jsonObj.getString("topic"));
stmt.setString(2, jsonObj.getString("category"));
stmt.setString(3, jsonObj.getString("question"));
stmt.setString(4, jsonObj.getString("answer"));
stmt.setString(5, jsonObj.getString("profilepic"));
stmt.setString(6, jsonObj.getString("url"));
stmt.setInt(7, jsonObj.getInt("id"));
但我得到以下错误
failureorg.postgresql.util.PSQLException: ERROR: syntax error at or near \"SET\"\n Position: 170"
我怎样才能使表格同时更新另一张表?
答案 0 :(得分:4)
您可以使用CTE执行此操作:
WITH i AS (
INSERT INTO TRENDING ( TOPIC , CATEGORY , QUESTION , ANSWER , PROFILEPIC , TICK , URL )
VALUES( ?, ? , ? , ? , ? , now() , ? )
Returning ID
)
UPDATE QUESTBANK
SET wasanswered = 'yes'
WHERE id IN (SELECT ID FROM i);