Sql 插入postgres不起作用
我讨厌问像这样愚蠢的小虫子,但我一辈子都搞不懂 此查询:Sql 插入postgres不起作用,sql,postgresql,Sql,Postgresql,我讨厌问像这样愚蠢的小虫子,但我一辈子都搞不懂 此查询: INSERT INTO user_authentication(init_password_setup) VALUES( substr((md5(random()::TEXT)), 0, 10) ) WHERE (init_password_setup = NULL OR init_password_setup ='') 正在抛出此错误: 16:27:11 Kernel error: ERROR: syntax error at o
INSERT INTO user_authentication(init_password_setup) VALUES( substr((md5(random()::TEXT)), 0, 10) ) WHERE (init_password_setup = NULL OR init_password_setup ='')
正在抛出此错误:
16:27:11 Kernel error: ERROR: syntax error at or near "WHERE"
我还尝试将其作为插入选择运行。衷心感谢您的帮助非常感谢
插入语句中没有where
子句。使用update
子句(可以有where
子句)插入新数据或更新现有数据。不能使用where子句进行插入。
如果您试图插入,则只需使用值。
如果要使用where,则使用UPDATE。如果要修改现有行,则需要UPDATE
语句,而不是INSERT
语句
Update将修改现有行,Insert将在表中添加新行
UPDATE user_authentication
SET init_password_setup = substr((md5(random()::TEXT)), 0, 10)
WHERE init_password_setup IS NULL
OR init_password_setup =''
INSERT语句没有WHERE子句。但是你可以使用插入。。。SELECT语句:
INSERT INTO user_authentication (user_id, init_password_setup)
SELECT id, substr((md5(random()::TEXT)), 0, 10)
FROM users
WHERE <some condition here>;
对不起,伙计们。非常愚蠢。不知道我在想什么。一天的晚些时候。感谢您的快速帮助。插入到。。。选择。。。其中
完全有效。问题是不能在值
集合表达式中使用WHERE
子句。也就是说,我同意用户看起来很困惑,似乎试图使用INSERT
来UPDATE
一行。这与@jurgend的回答是错误的
UPDATE user_authentication
SET init_password_setup = substr((md5(random()::TEXT)), 0, 10)
WHERE init_password_setup IS NULL OR init_password_setup ='';