简体   繁体   English

在另一个列表 PostgreSQL 中插入列的最后一个 id

[英]insert last id of column in another column table PostgreSQL

I'm trying to insert into a column code from table calcul the last id of column code from table stock, this is my request which doesn't work.我正在尝试将表库存中列代码的最后一个 id 从表计算插入到列代码中,这是我的请求,但不起作用。

  String sql1="INSERT INTO calcul (idproduit,inventaire,consomation,date,quantite,designation,dateper,ppa,tr,net,code) "
           + "VALUES ('"+codeP+"',"+newQuant+","+0+",'"+datestock+"',"+newQuant+",'"+designation+"','"+datePer+"',"+PPA+","+TR+","+NET+",SELECT MAX(code) from stock );";
     
       stmt.executeUpdate(sql1);

You need to modify your query to include a sub-query (replace values).您需要修改查询以包含子查询(替换值)。 I am not going to duplicate answers from across the stackoverflow in here however, I would like to give you reference to a few questions that already got answers, see here and here我不会在这里重复整个 stackoverflow 中的答案,但是我想给您参考一些已经得到答案的问题,请参阅此处此处

In your, I would write the query as follow:在您中,我会按如下方式编写查询:

"INSERT INTO calcul (idproduit,inventaire,consomation,date,quantite,designation,dateper,ppa,tr,net,code) SELECT '"+codeP+"',"+newQuant+","+0+",'"+datestock+"',"+newQuant+",'"+designation+"','"+datePer+"',"+PPA+","+TR+","+NET+",MAX(code) from stock; "INSERT INTO calcul (idproduit,inventaire,consomation,date,quantite,designation,dateper,ppa,tr,net,code) SELECT '"+codeP+"',"+newQuant+","+0+",'"+datestock+ "',"+newQuant+",'"+designation+"','"+datePer+"',"+PPA+","+TR+","+NET+",MAX(code) 现货;

Your query is a bit messy, so I want to put it in more generic form below你的查询有点乱,所以我想把它放在下面更通用的形式

INSERT INTO calcul (col1, col2, ....,coln) SELECT col1, col2, ..., MAX(code) FROM stock;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring、jdbcTemplate、postgresql - 最后插入 id - spring, jdbcTemplate, postgresql - last insert id 逐列将数据插入PostgreSQL中的行 - Insert data into row in PostgreSQL column by column 获取 mysql(innodb) AUTO_INCREMENT Column、JDBC/getGeneratedKeys()/last_insert_id (in OkPacket) 和 LAST_INSERT_ID() 的方法 - Ways to get mysql(innodb) AUTO_INCREMENT Column, JDBC/getGeneratedKeys()/last_insert_id (in OkPacket) and LAST_INSERT_ID() java,mysql-为什么不工作,从一个表中获取最后一个插入的ID,然后将其再次插入另一个表中? - java,mysql - why not working get last inserted id from one table and insert it again on another table? 获取第一个表中最后添加的id,然后使用java将其插入另一个表中 - get last added id in first table and insert it in another table using java mysql,java - 从一个表中获取最后插入的自动增量 id 并将其再次插入到另一个表中 - mysql, java - Get last inserted auto increment id from one table and insert it again on another table 如何使用rest api插入hibernate中的表,并且该表2列是另一个表的外键? - How to insert into a table in hibernate using rest api and on that table 2 column are foreign key of another table? 在新表中插入 id 列 - Inserting id column in new table 无法将值 Null 插入“ID”列 - Hibernate - Cannot insert the value Null into column “ID” - Hibernate 如何将数据插入到 id = ... 的现有列? - How to insert data to existing column where id = …?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM