如果值不为空,如何更新PostgreSQL中的列?[closed]

n9vozmp4  于 2022-11-04  发布在  PostgreSQL
关注(0)|答案(1)|浏览(119)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
9天前关闭。
Improve this question

UPDATE ${usersTable} SET age = COALESCE($2, age)

如果年龄为空,则保留当前值
我试着用COALESCE,但是它不起作用,请帮帮我

db.query(`UPDATE ${usersTable} SET age = COALESCE($2, age), password = COALESCE($3, password), sex = COALESCE($4, sex), tel = COALESCE($5, tel), avatar = COALESCE($6, avatar) WHERE email = $1 RETURNING *`, 
            [email, age, password, sex, tel, avatar])
4ioopgfo

4ioopgfo1#

如果年龄为空,则保留当前值
我不明白你的意思。列的值是多少?

db.query(`UPDATE ${usersTable}
SET age = CASE WHEN age IS NULL THEN age ELSE $2 END,
    password = CASE WHEN password IS NULL THEN age ELSE $3 END,
    sex = CASE WHEN sex IS NULL THEN age ELSE $4 END,
    tel = CASE WHEN tel IS NULL THEN age ELSE $5 END,
    avatar = CASE WHEN avatar IS NULL THEN age ELSE $6 END
WHERE email = $1 RETURNING *`, 
            [email, age, password, sex, tel, avatar])

如果你指的是变量age,那么我想你应该试着用编程语言中的'null'字符串值来代替null值。

相关问题