postgresql- select的公共表表达式更新列
我有此代码,该代码旨在为表中的所有列值生成哈希密码。
当我看到结果时,问题出现了,仅创建一个哈希(对表的行数重复),而不是列Cust_cif中的每个行/值一个不同。
CREATE EXTENSION pgcrypto;
ALTER TABLE pec4.tb_customer
ADD password VARCHAR (255);
WITH password_new AS (
SELECT cust_cif, crypt('cust_cif', gen_salt('md5')) AS pass
FROM pec4.tb_customer
)
UPDATE tb_customer
SET password = p.pass
FROM password_new p;
如果我执行查询(仅选择部分),则获得所需的输出(每行/值一个哈希):
SELECT cust_cif, crypt('cust_cif', gen_salt('md5')) AS pass
FROM pec4.tb_customer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新后,您可以(或应该?)检查密码哈希是否可以。
您可以使用此语句执行此操作,该语句应在上一列中返回
t
:您的语句只会返回
t
当您执行时:是否检查密码是否
'cust_cif'
,而不是如果密码是存储在字段中的值cust_cif
。您的语句缺少tb_customer和password_new表之间的链接。纠正工作的陈述应该是:
但是更容易编写:
或者,在问题中尚不清楚,是您打算将密码更新为'cust_cif'而不是列的价值
cust_cif <?
After the update you could (or should?) check if the password hash is OK.
You can do that using this statement, which should return
t
in the last column:Your statement will only return
t
when you do:which is checking if the password is
'cust_cif'
, and not if the password is the value stored in the fieldcust_cif
.Your statement is missing the link between tb_customer and the password_new table. Corrected to work your statement should be:
But it's easier to write:
Or, which is not clear in the question, was it your intention to update the password to 'cust_cif', and not to the value of the column
cust_cif
?