[英]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.