我有这两个表:
TableA
+----+----------+----------+
| Id | TableBId | TableCId |
+----+----------+----------+
| 1 | 1 | 10 |
| 2 | 2 | 11 |
| 3 | 3 | 12 |
| 4 | 4 | 13 |
| 5 | 5 | 14 |
TableC
+-----+------+
| Id | Name |
+-----+------+
| 10 | John |
| 11 | John |
| 12 | Ann |
| 13 | Mark |
| 14 | Mark |
+-----+------+
如何保留一个并从TableC
中删除重复记录并在TableA
中更改ID?
最后,表格应如下所示:
TableA
+----+----------+----------+
| Id | TableBId | TableCId |
+----+----------+----------+
| 1 | 1 | 10 |
| 2 | 2 | 10 |
| 3 | 3 | 12 |
| 4 | 4 | 13 |
| 5 | 5 | 13 |
TableC
+-----+------+
| Id | Name |
+-----+------+
| 10 | John |
| 12 | Ann |
| 13 | Mark |
+-----+------+
首先,我需要更改ID,然后删除重复项。
答案 0 :(得分:1)
首先更新表from scipy.optimize import minimize
x0 = np.array([1. for i in range(11)])
def my_function(A, B, C, D, E, F, G, H, I, J, K):
F1 = A + B + C + D - 200
F2 = C + D + E - 50
F3 = C + D + E + F + G - 45
F4 = E + F + G + H + I + J + K - 67
F5 = H + I + J + K - 64
return F1 + F2 +F3 +F4 + F5
cons = ({'type': 'ineq', 'my_function': lambda A, B, C, D: A + B + C + D - 200},
{'type': 'ineq', 'my_function': lambda C, D, E: C + D + E - 50},
{'type': 'ineq', 'my_function': lambda C, D, E, F, G: C + D + E + F + G - 45},
{'type': 'ineq', 'my_function': lambda E, F, G, H, I, J, K: E + F + G + H + I + J + K - 67},
{'type': 'ineq', 'my_function': lambda H, I, J, K: H + I + J + K - 64})
res = minimize(my_function, x0, method='BFGS', constraints=cons )
,方法是将表a
中的列cid
设置为最小值id
,该列c
与{{ 1}}等于name
:
id
然后从表cid
中删除所有具有重复名称的行,并保留具有最少update a
set cid = (
select min(id) from c where name = (select name from c where id = a.cid)
);
的行:
c
请参见demo。
结果:
表c
id
表a
delete from c t where exists (
select 1 from c where name = t.name and id < t.id
);