我在cockroachdb中有一张桌子,如下所示:
key | value
+-------------------+---------------------------------------------------------+
<host_ipa> | {“student_name": “John",“degree": “Bachelors”,“university”: “KU”,“Company”: “ABC"}
现在,我只想更新jsonb
-degree
和university
的两个键。我该怎么办?
我有一个解决方案,但需要提供所有四个字段。
这就是我在golang中拥有的东西:
sqlCmd := fmt.Sprintf(" UPDATE %v set value = json_object('{%v,%v,%v}'::string[],'{%v,%v}'::string[]) WHERE key = '%v' ",myTable,strconv.Quote("student_name”),strconv.Quote("degree"),strconv.Quote(“university”),strconv.Quote("Company"),strconv.Quote(student),strconv.Quote(newDegree),strconv.Quote(newUniversity),strconv.Quote(company),host)
db,err := sql.Open(dbDriver,DBConnStr)
_,err = db.Exec(sqlCmd)
基于@bergi的工作解决方案:
db,DBConnStr)
sqlCmd := "UPDATE " + myTable + " SET value = value || jsonb_build_object(‘degree',$1::string,‘university',$2::string) WHERE key = $3"
_,err = db.Exec(sqlCmd,newDegree,newUniversity,host)