postgre sql 批量更新 根据另一张表中的yxj优先级列 去更新另一个表中的yxj优先级列
SELECT
c_bh,
c_zdmc,
ROW_NUMBER () OVER (
PARTITION BY c_zdmc
ORDER BY
n_zxd DESC
) AS yxj2
FROM
db_jcwzsj.t_zjzdyxj;
UPDATE db_jcwzsj.t_zjzdyxj AS P
SET n_yxj = A .yxj2
FROM
(
SELECT
c_bh,
c_zdmc,
ROW_NUMBER () OVER (
PARTITION BY c_zdmc
ORDER BY
n_zxd DESC
) AS yxj2
FROM
db_jcwzsj.t_zjzdyxj
) AS A
WHERE
P .c_bh = A .c_bh;
postgresql如果查询出的数据为空,则设为0的方法
select COALESCE(b.price, 0) as price from fruit_sale b
postgresql多表联合批量更新
update table_p as p
set p_name = a.name,p_user = a.user_id,p_type = 'P02'
from (
select x.user_id,x.name,x.p_id from table_u x
join table_p y on x.p_id = y.p_id
) as a where p.p_id = a.p_id;
批量插入 解决对数据库频繁开销的问题
/**
* 批量插入Zdyxj
*/
public void updateBatchZdyxj(List<Zdyxjs> list) {
StringBuilder sql = new StringBuilder();
final List<Zdyxjs> tempBpplist = list;
sql.append("INSERT INTO db_jcwzsj.t_zdyxj VALUES(?,?,?,?,?,?,?,?,?,?,?)");
abaseconn.batchUpdate(sql.toString(),new BatchPreparedStatementSetter() {
@Override
public int getBatchSize() {
return tempBpplist.size();
}
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setString(1, tempBpplist.get(i).getCbh());
ps.setString(2, tempBpplist.get(i).getCbhzt());
ps.setString(3, tempBpplist.get(i).getCbhysjb());
ps.setString(4, tempBpplist.get(i).getCbhysjbzd());
ps.setString(5, tempBpplist.get(i).getCzsjbzdmc());
ps.setInt(6, tempBpplist.get(i).getNyxj());
ps.setDouble(7, tempBpplist.get(i).getNzxd());
ps.setString(8, tempBpplist.get(i).getCkxly());
ps.setInt(9, tempBpplist.get(i).getNorder());
ps.setString(10, tempBpplist.get(i).getCbhztrhb());
ps.setString(11, tempBpplist.get(i).getCmrz());
}
});
}
/**
* 清空质检优先级表
*/
public void clearZjZdyxj() {
List<String> tables = new ArrayList<String>();
tables.add(".t_zjzdyxj");
String[] sqls = new String[tables.size()];
for (int i = 0; i < tables.size(); i++) {
sqls[i] = "truncate table " + Constants.ZSJ_SCHEMA + tables.get(i);
}
abaseconn.batchUpdate(sqls);
}
https://blog.****.net/qq_33269520/article/details/79727961更多例子