postgresql 将字段与值进行比较并返回布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10972730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:59:20  来源:igfitidea点击:

Compare field with value and return bool

sqlpostgresqlnpgsql

提问by Arman Hayots

I'm trying move hash cheking from server app to PostgreSQL. In other words I'm need calling a query to PGSQL, which compare string from query with string from field and return result of equal comparison as bool, but I don't know how do this without procedures on clear SQL.

我正在尝试将哈希检查从服务器应用程序移动到 PostgreSQL。换句话说,我需要向 PGSQL 调用一个查询,它将查询中的字符串与字段中的字符串进行比较,并将相等比较的结果返回为 bool,但我不知道如果没有清除 SQL 的过程,我该怎么做。

upd: I have table userswith field password(currently text, in the future - bytea). I want write sometihng like

upd:我有带有字段密码的用户(当前为text,将来为bytea)。我想写点东西

select * from values ('WrittenPassword' = users.password) where username = 'someuser' ,

which must return true or false as result of equal comparison.

作为相等比较的结果,它必须返回 true 或 false

回答by Zane Bien

You can use a CASEstatement to return certain values based on a condition:

您可以使用CASE语句根据条件返回某些值:

SELECT 
    (CASE WHEN password = 'WrittenPassword' THEN 1 ELSE 0 END) AS is_equal
FROM
    users
WHERE
    username = 'someuser'