postgresql- select的公共表表达式更新列

发布于 2025-02-01 23:10:48 字数 706 浏览 4 评论 0 原文

我有此代码,该代码旨在为表中的所有列值生成哈希密码。

当我看到结果时,问题出现了,仅创建一个哈希(对表的行数重复),而不是列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

“在此处输入图像说明”

I have this code which intents to generate a hash password for all values of a column in a table.

The issue comes when I see the results, only one hash is created (repeated for the number of rows of the table), instead of one different per row/value in column 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;

If I execute the query (only the select part), I get the output I want (one hash per row/value):

SELECT cust_cif, crypt('cust_cif', gen_salt('md5')) AS pass
FROM pec4.tb_customer

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2025-02-08 23:10:48

更新后,您可以(或应该?)检查密码哈希是否可以。

您可以使用此语句执行此操作,该语句应在上一列中返回 t

select t.*, (password=crypt(cust_cif,password)) from tb_customer t;

您的语句只会返回 t 当您执行时:

select t.*, (password=crypt('cust_cif',password)) from tb_customer t;

是否检查密码是否'cust_cif',而不是如果密码是存储在字段中的值 cust_cif

您的语句缺少tb_customer和password_new表之间的链接。纠正工作的陈述应该是:

WITH password_new AS (
    SELECT cust_cif, crypt(cust_cif, gen_salt('md5')) AS pass
FROM tb_customer
)
UPDATE tb_customer 
SET password = p.pass
FROM password_new p
WHERE p.cust_cif = tb_customer.cust_cif;

但是更容易编写:

update tb_customer set password=crypt(cust_cif, gen_salt('md5'));

或者,在问题中尚不清楚,是您打算将密码更新为'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:

select t.*, (password=crypt(cust_cif,password)) from tb_customer t;

Your statement will only return t when you do:

select t.*, (password=crypt('cust_cif',password)) from tb_customer t;

which is checking if the password is 'cust_cif', and not if the password is the value stored in the field cust_cif.

Your statement is missing the link between tb_customer and the password_new table. Corrected to work your statement should be:

WITH password_new AS (
    SELECT cust_cif, crypt(cust_cif, gen_salt('md5')) AS pass
FROM tb_customer
)
UPDATE tb_customer 
SET password = p.pass
FROM password_new p
WHERE p.cust_cif = tb_customer.cust_cif;

But it's easier to write:

update tb_customer set password=crypt(cust_cif, gen_salt('md5'));

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 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文