PostgreSQL,从列中插入值

时间:2019-05-15 19:54:03

标签: sql postgresql

现在,我有这张桌子:

id|name|status (varchar)|fk_id_test (int)
-----------------------------------------
1 |ABC | X              |
2 | DX | Y              |
3 | ZZ | X              |
4 | TU | Y              |
5 | EE | Z              |

我必须插入fk_id_test列中的status值。

算法类似于:

switch(status)
    case 'X': `fk_ik_test` = 1
    case 'Y': `fk_ik_test` = 2
    case 'Z': `fk_ik_test` = 3
etc.

结果应为:

现在,我有这张桌子:

id|name|status (varchar)|fk_id_test (int)
-----------------------------------------
1 |ABC | X              |1
2 | DX | Y              |2
3 | ZZ | X              |1
4 | TU | Y              |2
5 | EE | Z              |3

我有5个status。这是因为我将使用另一个表(测试)以及这些状态的名称(varchar)。

有可能在postgresql中这样做吗?

3 个答案:

答案 0 :(得分:1)

您需要更新

//imports

object PlayGround {
def missingValType2(n: Int):Int = {
    if(n == null){
      0
    }else{
      n
    }
  }

   def main(args: Array[String]): Unit = {

    Logger.getLogger("org").setLevel(Level.ERROR)
    val spark = SparkSession
      .builder()
      .appName("PlayGround")
      .config("spark.sql.warehouse.dir", "file:///C:/temp")
      .master("local[*]")
      .getOrCreate()

    val missingValUDFType2 = udf[Int, Int](missingValType2)

     val schema = List(
      StructField("name", types.StringType, false),
      StructField("age", types.IntegerType, true)
    )

    val data = Seq(
      Row("miguel", null),
      Row("luisa", 21)
    )
    val df = spark.createDataFrame(
      spark.sparkContext.parallelize(data),
      StructType(schema)
    )
    df.show(false)
    df.withColumn("ageNullReplace",missingValUDFType2($"age")).show()

  }
}

/**
  * +------+----+
  * |name  |age |
  * +------+----+
  * |miguel|null|
  * |luisa |21  |
  * +------+----+
  *
  * Below is the current output.
  * +------+----+--------------+
  * |  name| age|ageNullReplace|
  * +------+----+--------------+
  * |miguel|null|          null|
  * | luisa|  21|            21|
  * +------+----+--------------+*/

答案 1 :(得分:1)

一种方法将update与连接一起使用:

update t
    set fk_id_test = v.fk_id_test
    from (values ('X', 1), ('Y', 2), ('Z', 3)) v(status, fk_id_test)
    where v.status = t.status;

答案 2 :(得分:0)

您要查找case表达式吗?

case status
    when 'X' then 1
    when 'Y' then 2
    when 'Z' then 3
etc.

end as fk_ik_test