通过 postgresql 中的其他列值更新列值
update column values by other column values in postgresql
我有什么
id s_1 s_2 s_3 s_4 s_5 s_6 s_7 s_8
ax 1 0 0 0 0 0 0 0
bx 0 1 0 0 0 0 0 0
cx 0 0 1 0 0 0 0 0
dx 0 0 0 1 0 0 0 0
ex 0 0 0 0 1 0 0 0
fx 0 0 0 0 0 1 0 0
gx 0 0 0 0 0 0 1 0
hx 0 0 0 0 0 0 0 1
我正在尝试更新我的列值,如下所示。
如果我有 1,0,0,0,0,0,0,0 作为从 s1 到 s8 的列值,那么它应该更新为 1,0,1,0,1,0,1 ,0
如果我有 0,1,0,0,0,0,0,0 作为从 s1 到 s8 的列值,那么它应该更新为 0,1,1,0,1,0,1, 0
如果我有 0,0,1,0,0,0,0,0 作为从 s1 到 s8 的列值,那么它应该更新为 0,0,1,0,1,0,1, 0
如果我有 0,0,0,1,0,0,0,0 作为从 s1 到 s8 的列值,那么它应该更新为 0,0,0,1,1,0,1, 0
如果我有 0,0,0,0,1,0,0,0 作为从 s1 到 s8 的列值,那么它应该更新为 0,0,0,0,1,0,1 ,0
如果我有 0,0,0,0,0,1,0,0 作为从 s1 到 s8 的列值,那么它应该更新为 0,0,0,0,1,1,1, 0
如果我有 0,0,0,0,0,0,1,0 作为从 s1 到 s8 的列值,那么它应该更新为 0,0,0,0,0,0,1 ,0
如果我有 0,0,0,0,0,0,0,1 作为从 s1 到 s8 的列值,那么它应该更新为 0,0,0,0,0,0,0, 1
输出如下所示
id s_1 s_2 s_3 s_4 s_5 s_6 s_7 s_8
ax 1 0 1 0 1 0 1 0
bx 0 1 1 0 1 0 1 0
cx 0 0 1 0 1 0 1 0
dx 0 0 0 1 1 0 1 0
ex 0 0 0 0 1 0 1 0
fx 0 0 0 0 1 1 1 0
gx 0 0 0 0 0 0 1 0
hx 0 0 0 0 0 0 0 1
我确定那里有一个 SQL 更新解决方案,但下面的多语句更容易编写(和调试):
update my_table -- case 1: 1,0,0,0,0,0,0,0
set s1 = 1, s2 = 0, s3 = 1, s4 = 0, s5 = 1, s6 = 0, s7 = 1, s8 = 0
where s1 = 1 and s2 = 0 and s3 = 0 and s4 = 0
and s5 = 0 and s6 = 0 and s7 = 0 and s8 = 0;
那么,对于情况 2:
update my_table -- case 2: 0,1,0,0,0,0,0,0
set s1 = 0, s2 = 1, s3 = 1, s4 = 0, s5 = 1, s6 = 0, s7 = 1, s8 = 0
where s1 = 0 and s2 = 1 and s3 = 0 and s4 = 0
and s5 = 0 and s6 = 0 and s7 = 0 and s8 = 0;
你可以写剩下的案例(3、4、5、6、7、8)。
UPDATE table_name AS o
SET (s_1, s_2, s_3, s_4, s_5, s_6, s_7, s_8) = (n_1, n_2, n_3, n_4, n_5, n_6, n_7, n_8)
FROM (
VALUES
(1,0,0,0,0,0,0,0, 1,0,1,0,1,0,1,0),
(0,1,0,0,0,0,0,0, 0,1,1,0,1,0,1,0),
(0,0,1,0,0,0,0,0, 0,0,1,0,1,0,1,0),
(0,0,0,1,0,0,0,0, 0,0,0,1,1,0,1,0),
(0,0,0,0,1,0,0,0, 0,0,0,0,1,0,1,0),
(0,0,0,0,0,1,0,0, 0,0,0,0,1,1,1,0),
(0,0,0,0,0,0,1,0, 0,0,0,0,0,0,1,0),
(0,0,0,0,0,0,0,1, 0,0,0,0,0,0,0,1)
) as n (
s_1, s_2, s_3, s_4, s_5, s_6, s_7, s_8,
n_1, n_2, n_3, n_4, n_5, n_6, n_7, n_8
)
WHERE (o.s_1, o.s_2, o.s_3, o.s_4, o.s_5, o.s_6, o.s_7, o.s_8) = (n.s_1, n.s_2, n.s_3, n.s_4, n.s_5, n.s_6, n.s_7, n.s_8)
我有什么
id s_1 s_2 s_3 s_4 s_5 s_6 s_7 s_8
ax 1 0 0 0 0 0 0 0
bx 0 1 0 0 0 0 0 0
cx 0 0 1 0 0 0 0 0
dx 0 0 0 1 0 0 0 0
ex 0 0 0 0 1 0 0 0
fx 0 0 0 0 0 1 0 0
gx 0 0 0 0 0 0 1 0
hx 0 0 0 0 0 0 0 1
我正在尝试更新我的列值,如下所示。
如果我有 1,0,0,0,0,0,0,0 作为从 s1 到 s8 的列值,那么它应该更新为 1,0,1,0,1,0,1 ,0
如果我有 0,1,0,0,0,0,0,0 作为从 s1 到 s8 的列值,那么它应该更新为 0,1,1,0,1,0,1, 0
如果我有 0,0,1,0,0,0,0,0 作为从 s1 到 s8 的列值,那么它应该更新为 0,0,1,0,1,0,1, 0
如果我有 0,0,0,1,0,0,0,0 作为从 s1 到 s8 的列值,那么它应该更新为 0,0,0,1,1,0,1, 0
如果我有 0,0,0,0,1,0,0,0 作为从 s1 到 s8 的列值,那么它应该更新为 0,0,0,0,1,0,1 ,0
如果我有 0,0,0,0,0,1,0,0 作为从 s1 到 s8 的列值,那么它应该更新为 0,0,0,0,1,1,1, 0
如果我有 0,0,0,0,0,0,1,0 作为从 s1 到 s8 的列值,那么它应该更新为 0,0,0,0,0,0,1 ,0
如果我有 0,0,0,0,0,0,0,1 作为从 s1 到 s8 的列值,那么它应该更新为 0,0,0,0,0,0,0, 1
输出如下所示
id s_1 s_2 s_3 s_4 s_5 s_6 s_7 s_8
ax 1 0 1 0 1 0 1 0
bx 0 1 1 0 1 0 1 0
cx 0 0 1 0 1 0 1 0
dx 0 0 0 1 1 0 1 0
ex 0 0 0 0 1 0 1 0
fx 0 0 0 0 1 1 1 0
gx 0 0 0 0 0 0 1 0
hx 0 0 0 0 0 0 0 1
我确定那里有一个 SQL 更新解决方案,但下面的多语句更容易编写(和调试):
update my_table -- case 1: 1,0,0,0,0,0,0,0
set s1 = 1, s2 = 0, s3 = 1, s4 = 0, s5 = 1, s6 = 0, s7 = 1, s8 = 0
where s1 = 1 and s2 = 0 and s3 = 0 and s4 = 0
and s5 = 0 and s6 = 0 and s7 = 0 and s8 = 0;
那么,对于情况 2:
update my_table -- case 2: 0,1,0,0,0,0,0,0
set s1 = 0, s2 = 1, s3 = 1, s4 = 0, s5 = 1, s6 = 0, s7 = 1, s8 = 0
where s1 = 0 and s2 = 1 and s3 = 0 and s4 = 0
and s5 = 0 and s6 = 0 and s7 = 0 and s8 = 0;
你可以写剩下的案例(3、4、5、6、7、8)。
UPDATE table_name AS o
SET (s_1, s_2, s_3, s_4, s_5, s_6, s_7, s_8) = (n_1, n_2, n_3, n_4, n_5, n_6, n_7, n_8)
FROM (
VALUES
(1,0,0,0,0,0,0,0, 1,0,1,0,1,0,1,0),
(0,1,0,0,0,0,0,0, 0,1,1,0,1,0,1,0),
(0,0,1,0,0,0,0,0, 0,0,1,0,1,0,1,0),
(0,0,0,1,0,0,0,0, 0,0,0,1,1,0,1,0),
(0,0,0,0,1,0,0,0, 0,0,0,0,1,0,1,0),
(0,0,0,0,0,1,0,0, 0,0,0,0,1,1,1,0),
(0,0,0,0,0,0,1,0, 0,0,0,0,0,0,1,0),
(0,0,0,0,0,0,0,1, 0,0,0,0,0,0,0,1)
) as n (
s_1, s_2, s_3, s_4, s_5, s_6, s_7, s_8,
n_1, n_2, n_3, n_4, n_5, n_6, n_7, n_8
)
WHERE (o.s_1, o.s_2, o.s_3, o.s_4, o.s_5, o.s_6, o.s_7, o.s_8) = (n.s_1, n.s_2, n.s_3, n.s_4, n.s_5, n.s_6, n.s_7, n.s_8)