通过PostgreSQL中的其他列值更新列值

时间:2018-07-05 23:02:04

标签: sql sql-update greenplum

我有什么

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 < / p>

如果我有0,0,1,0,0,0,0,0作为从s1到s8的列值,则应将其更新为0,0,1,0,1,0,1,0 < / p>

如果我有0,0,0,1,0,0,0,0作为从s1到s8的列值,则应将其更新为0,0,0,1,1,0,1,0 < / p>

如果我有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 < / p>

如果我有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,0,1 < / p>

输出如下所示

 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

2 个答案:

答案 0 :(得分:1)

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)

答案 1 :(得分:0)

我确定那里有一个SQL更新解决方案,但是以下多语句更容易编写(和调试):

    import sys
    from  PyQt5 import QtGui, QtCore, uic, QtWidgets
    from PyQt5.QtWidgets import QApplication, QWidget
    file="PID GUI.py"

    Ui_MainWindow, QtBaseClass= uic.loadUiType(file)

    class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.show()

    if __name__=="__main__":

        app=QApplication(sys.argv)
        ex=MyApp()
        sys.exit(app.exec_())

然后,对于情况2:

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;

您可以编写其余情况(3、4、5、6、7和8)。