带有 Java preparedstatement 的 Postgres INSERT INTO where 子句

Postgres INSERT INTO where clause with Java preparedstatement

我正在尝试在 table 中插入数据,在 Java 和 postgres JDBC 中使用 PreparedStatement。 我想插入列开始日期和列结束日期值,条件是给出房间号 (?);

准备语句 stm = conn.prepareStatement( "INSERT INTO room(startdate,enddate) values(?,?) WHERE roomnumber = ?"))

它在 WHERE 处给我一个错误。 什么是正确的语法。 非常感谢

使用 UPDATE 更改现有行,使用 INSERT 创建新行。 文档中有许多示例:https://www.postgresql.org/docs/9.5/static/sql-update.html

例如:UPDATE room SET startdate = ?, enddate = ? WHERE roomnumber = ?

你可以使用类似的东西:

insert into room(startdate, enddate) select '2020-01-01'::date, '2020-02-11'::date where (select roomnumber = ? from roomnumber_table);

另请查看:https://www.postgresql.org/docs/12/queries-with.html

使用 Insert Into Select 而不是 Insert Into Values

INSERT INTO table1(firstname, lastname)
    select 'Sally', 'Brown'
    Where exists (Select 1 from table2 where field1=val1 )