多表关联更新

最后更新:2024-09-11 05:30:01 | 状态:未完成
先创建测试表
/**
@Test
public void init() throws Exception {
	init("FI_USER");
	init("HR_USER");
	init("MM_USER");
}
public void init(String name) throws Exception {
	Table table = ServiceProxy.metadata().table(name, false);
	if(null != table){
		ServiceProxy.ddl().drop(table);
	}
	table = new Table(name);
	table.addColumn("ID", "BIGINT").setPrimary(true).setAutoIncrement(true);
	table.addColumn("CODE", "VARCHAR(32)");
	table.addColumn("NAME", "VARCHAR(10)");
	table.addColumn("TYPE_CODE", "VARCHAR(10)");
	table.addColumn("LVL", "INT");
	table.addColumn("REMARK", "VARCHAR(100)");
	ServiceProxy.ddl().create(table);
}

关联多表更新

RunPrepare prepare = TableBuilder.init("FI_USER AS FI")
		.left("HR_USER AS HR", "HR.ID = FI.ID")
		.build();
DataRow data = new DataRow();
data.put("CODE", 1);
data.putVar("NAME", "HR.NAME");
//过滤条件也可以通过TableBuilder或RunPrepare设置
ConfigStore configs = new DefaultConfigStore();
configs.and("FI.ID > 10");
ServiceProxy.service().update(prepare, data, configs, "HR.TYPE_CODE:100");
对应执行SQL:
--PG:
UPDATE FI_USER AS FI
LEFT JOIN HR_USER AS HR ON HR.ID = FI.ID
SET
	FI.CODE = ?, FI.NAME = HR.NAME
WHERE (HR.TYPE_CODE = ? AND FI.ID > 10)

--MySQL:
UPDATE FI_USER AS FI
LEFT JOIN HR_USER AS HR ON HR.ID = FI.ID
SET
FI.CODE = ?, FI.NAME = HR.NAME
WHERE (HR.TYPE_CODE = ? AND FI.ID > 10)

--SQL Server:
UPDATE FI SET
CODE = ?, NAME = HR.NAME
FROM FI_USER FI
LEFT JOIN HR_USER HR ON HR.ID = FI.ID
WHERE (HR.TYPE_CODE = ? AND FI.ID > 10)

--Oracle  注意Oracle因为用到子查询所以关联条件不要放到最后(外层)
UPDATE FI_USER FI SET 
(FI.CODE, FI.NAME) = ( SELECT ?, HR.NAME
	FROM HR_USER HR
	WHERE HR.ID = FI.ID
)
WHERE FI.ID > 10

关联子查询更新

RunPrepare inner_hr = TableBuilder.init("HR_USER(ID AS HR_ID, CODE AS HR_CODE, NAME) AS HR").build();
RunPrepare master = TableBuilder.init("FI_USER AS M")   //()内指定的是最外层的查询列名,放在主表名容易误解,可以addColumns()单独指定
		.left("HRS", inner_hr, "HRS.HR_ID = M.ID", "HRS.HR_CODE = M.CODE")    //主表的表名列名要用原名 这里的子查的表名列名注意用 别名 HRS是当前子查询的别名
		.left("MM_USER AS MM", "MM.ID = HRS.HR_ID")
	.build();
DataRow data = new DataRow();
data.put("CODE", 1);
data.putVar("NAME", "HRS.NAME");
ConfigStore configs = new DefaultConfigStore();
configs.and("M.ID > 10");
ServiceProxy.service().update(master, data, configs, "M.TYPE_CODE:100");
对应执行SQL:
--PG:
UPDATE FI_USER M
SET
CODE = ?, NAME = HRS.NAME
FROM (
	SELECT 
		ID HR_ID, CODE HR_CODE, NAME
	FROM HR_USER HR
) HRS
LEFT JOIN MM_USER MM ON MM.ID = HRS.HR_ID
WHERE (M.ID > 10
		 AND (HRS.HR_ID = M.ID AND HRS.HR_CODE = M.CODE))

--MySQL:
UPDATE FI_USER AS M
LEFT JOIN (
	SELECT 
		ID AS HR_ID, CODE AS HR_CODE, NAME
	FROM HR_USER AS HR
) AS HRS ON (HRS.HR_ID = M.ID AND HRS.HR_CODE = M.CODE)
LEFT JOIN MM_USER AS MM ON MM.ID = HRS.HR_ID
SET
M.CODE = ?, M.NAME = HRS.NAME
WHERE (M.TYPE_CODE = ? AND M.ID > 10)

--SQL Server:
UPDATE M SET
CODE = ?, NAME = HRS.NAME
FROM FI_USER M
LEFT JOIN (
	SELECT 
		ID HR_ID, CODE HR_CODE, NAME
	FROM HR_USER HR
) HRS ON (HRS.HR_ID = M.ID AND HRS.HR_CODE = M.CODE)
LEFT JOIN MM_USER MM ON MM.ID = HRS.HR_ID
WHERE (M.TYPE_CODE = ? AND M.ID > 10)

--Oracle
UPDATE FI_USER M SET 
(M.CODE, M.NAME) = ( SELECT ?, HRS.NAME
	FROM (
		SELECT 
			ID HR_ID, CODE HR_CODE, NAME
		FROM HR_USER HR
	) HRS
	LEFT JOIN MM_USER MM ON MM.ID = HRS.HR_ID
	WHERE (HRS.HR_ID = M.ID AND HRS.HR_CODE = M.CODE)
)
WHERE (M.TYPE_CODE = ? AND M.ID > 10)
首页 最近更新 搜索 提交 回复