基于CASE WHEN更新视图

时间:2015-10-22 09:45:35

标签: sql postgresql psql

我想更改表格中每列的值。 这就是我得到的:

CREATE VIEW old_employee AS
  SELECT *
    FROM dblink('dbname=mydb', 'select study, gloves, apron, vocation from personnel')
    AS t1(study char(10), gloves integer, apron integer, vocation integer);

SELECT * FROM old_employee;

   study    |  gloves | apron  | vocation 
------------+------------------+------------+
 STUDY1     |     1   |   1    |     1      |
 STUDY2     |     2   |   2    |     2      |
 STUDY3     |     0   |   2    |     3      |
 STUDY3     |     1   |   0    |     4      |

我想要的输出

   study    |  gloves | apron  | vocation 
------------+------------------+------------+
      1     |     51   |   54  |     35      |
      2     |     53   |   56  |     37      |
      3     |     52   |   56  |     43      |
      4     |     51   |   55  |     45      |

我的第一个想法是在更新时使用多个CASE WHEN。类似的东西:

UPDATE old_employee
   SET  study = (
                 SELECT studies.id
                 FROM studies
                 INNER JOIN old_employee
                 ON studies.name_string = old_employee.study
                ),
      gloves = (
              SELECT CASE 
                WHEN gloves = 0 THEN 52
                WHEN gloves = 1 THEN 51
                WHEN gloves = 2 THEN 53
                     END
               FROM old_employee
             ),
        apron = (
                SELECT CASE 
                    WHEN apron = 0 THEN 55
                    WHEN apron = 1 THEN 54
                    WHEN apron = 2 THEN 56
                END
                FROM old_employee
               ),
      vocation = (
                SELECT CASE 
                    WHEN vocation = 1 THEN 35
                    WHEN vocation = 2 THEN 37
                    WHEN vocation = 3 THEN 43
                    WHEN vocation = 4 THEN 45           
                END
                FROM old_employee
                );

显然这不起作用,我想知道我是否正确地处理了这个问题或者遗漏了什么。我是一名程序员,也不熟悉SQL,所以我还没有完全习惯于SQL-mindset。

1 个答案:

答案 0 :(得分:1)

虽然postgesql 9.3中的视图是可更新的,但我认为这是不好的做法,特别是在使用远程数据库时,因为它会改变原始表。所以我想说如果你不确定你在做什么,最好更新基础表。

此外,如果您运行早期版本的postgresql视图不可更新。

您可以将数据转换添加到视图定义中,而不是更新视图。

CREATE VIEW old_employee AS
  SELECT studies.id, 
         (SELECT CASE 
             WHEN t1.gloves = 0 THEN 52
             WHEN t1.gloves = 1 THEN 51
             WHEN t1.gloves = 2 THEN 53
         END) as gloves, 
         (SELECT CASE 
                    WHEN t1.apron = 0 THEN 55
                    WHEN t1.apron = 1 THEN 54
                    WHEN t1.apron = 2 THEN 56
          END) as apron,
          (
        SELECT CASE 
            WHEN t1.vocation = 1 THEN 35
            WHEN t1.vocation = 2 THEN 37
            WHEN t1.vocation = 3 THEN 43
            WHEN t1.vocation = 4 THEN 45           
        END) as vocation
    FROM studies INNER JOIN dblink('dbname=mydb', 'select study, gloves, apron, vocation from personnel')
    AS t1(study char(10), gloves integer, apron integer, vocation integer)
    ON studies.name_string = t1.study;