【问题标题】:Postgres INSERT INTO where clause with Java preparedstatement带有Javapreparedstatement的Postgres INSERT INTO where子句
【发布时间】:2018-02-25 12:54:36
【问题描述】:

我正在尝试使用 Java 中的 PreparedStatement 和 postgres JDBC 在表中插入数据。 我想插入列 startdate 和列 enddate 值,条件是给定房间号 (?);

PreparedStatement stm = conn.prepareStatement( "INSERT INTO room(startdate,enddate) values(?,?) WHERE roomnumber = ?"))

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

【问题讨论】:

    标签: java postgresql prepared-statement where-clause


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      你可以使用类似的东西:

      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

      【讨论】:

        【解决方案3】:

        使用插入到选择中而不是插入到值中

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-10-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-20
          • 1970-01-01
          • 2013-03-20
          相关资源
          最近更新 更多