postgre 循环关联条件update一个表的 数据到另一个表

原因:需要批量更新 把A表的某个字段值 替换到B表的某个字段值

我得具体例子:

with 是临时表 , update 关键点在于 from !!!!!! 还能这么写!!!!
update t_temp_kind_field_20211029 set c_relation_id = tt.c_task_id from temp_1029 tt where tt.c_id = c_relation_id;

----更新t_temp_kind_field_20211029
with temp_1029  as (
			select c_id,c_task_id from t_temp_module_20211029 where c_task_id in (SELECT
				c_id
			FROM
				t_temp_stage_task
			WHERE
				c_id IN (
					SELECT
						c_task_id
					FROM
						t_temp_module
					WHERE
						c_id IN (
							SELECT
								c_relation_id
							FROM
								t_temp_kind_field
							WHERE
								c_name LIKE '%诉讼/仲裁请求%'
							AND to_number(c_relation_id, '999999999') < 2000
						)
					AND c_name LIKE '%请求%'
				)
			AND to_number(c_task_type, '9999999999') < 50
			AND c_name LIKE '%申请和解、调解%')
)


--select tt.* from t_temp_kind_field kk,temp_1029 tt where kk.c_relation_id = tt.c_id
update t_temp_kind_field_20211029  set c_relation_id = tt.c_task_id from temp_1029 tt where tt.c_id = c_relation_id;

---更新t_temp_module_20211029
with temp_1029  as (
			select c_id,c_task_id from t_temp_module_20211029 where c_task_id in (SELECT
				c_id
			FROM
				t_temp_stage_task
			WHERE
				c_id IN (
					SELECT
						c_task_id
					FROM
						t_temp_module
					WHERE
						c_id IN (
							SELECT
								c_relation_id
							FROM
								t_temp_kind_field
							WHERE
								c_name LIKE '%诉讼/仲裁请求%'
							AND to_number(c_relation_id, '999999999') < 2000
						)
					AND c_name LIKE '%请求%'
				)
			AND to_number(c_task_type, '9999999999') < 50
			AND c_name LIKE '%申请和解、调解%')
)


update t_temp_module_20211029 set c_task_id = null where c_id in (
select c_id from t_temp_module_20211029 where c_task_id in (SELECT
				c_id
			FROM
				t_temp_stage_task
			WHERE
				c_id IN (
					SELECT
						c_task_id
					FROM
						t_temp_module
					WHERE
						c_id IN (
							SELECT
								c_relation_id
							FROM
								t_temp_kind_field
							WHERE
								c_name LIKE '%诉讼/仲裁请求%'
							AND to_number(c_relation_id, '999999999') < 2000
						)
					AND c_name LIKE '%请求%'
				)
			AND to_number(c_task_type, '9999999999') < 50
			AND c_name LIKE '%申请和解、调解%'))



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先需要安装相应的Python库:`psycopg2`和`mysql-connector-python`。 然后,可以按照以下步骤将PostgreSQL中的数据传输到MySQL中: 1. 连接到PostgreSQL数据库。 ```python import psycopg2 # 连接到PostgreSQL数据库 conn_pg = psycopg2.connect( host="localhost", database="mydb", user="myuser", password="mypassword" ) ``` 2. 从PostgreSQL中选择要传输的数据。 ```python # 从PostgreSQL中选择要传输的数据 cur_pg = conn_pg.cursor() cur_pg.execute("SELECT * FROM mytable") data = cur_pg.fetchall() ``` 3. 连接到MySQL数据库。 ```python import mysql.connector # 连接到MySQL数据库 conn_mysql = mysql.connector.connect( host="localhost", user="myuser", password="mypassword", database="mydb" ) ``` 4. 将数据插入到MySQL中。 ```python # 将数据插入到MySQL中 cur_mysql = conn_mysql.cursor() for row in data: cur_mysql.execute("INSERT INTO mytable VALUES (%s, %s, %s)", row) conn_mysql.commit() # 关闭连接 cur_pg.close() cur_mysql.close() conn_pg.close() conn_mysql.close() ``` 完整代码示例: ```python import psycopg2 import mysql.connector # 连接到PostgreSQL数据库 conn_pg = psycopg2.connect( host="localhost", database="mydb", user="myuser", password="mypassword" ) # 从PostgreSQL中选择要传输的数据 cur_pg = conn_pg.cursor() cur_pg.execute("SELECT * FROM mytable") data = cur_pg.fetchall() # 连接到MySQL数据库 conn_mysql = mysql.connector.connect( host="localhost", user="myuser", password="mypassword", database="mydb" ) # 将数据插入到MySQL中 cur_mysql = conn_mysql.cursor() for row in data: cur_mysql.execute("INSERT INTO mytable VALUES (%s, %s, %s)", row) conn_mysql.commit() # 关闭连接 cur_pg.close() cur_mysql.close() conn_pg.close() conn_mysql.close() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值