PostgreSQL中有任何哈希函数吗? [英] Is there any hash function in PostgreSQL?

查看:847
本文介绍了PostgreSQL中有任何哈希函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sphinx索引我的数据库。
问题是我必须按字符变化字段过滤结果。
因此,我必须找到一种将字符转换为 sql_attr_uint 的方法。
我知道mysql中的CRC32可以解决问题。

解决方案

也许您可以使用 decode(substring(md5)? ('foo')for 8),'hex')

您可以使用以下方式将其转换为整数:

 创建函数bytea_to_integer(bytea)
返回整数严格
语言sql作为$$
select
(get_byte($ 1 ,0)* 1 :: integer<< 0 * 8)
+(get_byte($ 1,1)* 1 :: integer<< 1 * 8)
+(get_byte($ 1,2 )* 1 :: integer<< 2 * 8)
+(get_byte($ 1,3)* 1 :: integer<< 3 * 8);
$$;


I am using Sphinx to index my database. The problem is I have to filter the result by a character varying field. So I have to find a way to convert character varying to sql_attr_uint. I know that CRC32 in mysql can do the trick. Is there a CRC32 or any replacement in PostgreSQL?

解决方案

Maybe you can use decode(substring(md5('foo') for 8), 'hex'). This would get you bytea of first 4 bytes of md5 hash of this string.

You can convert it to integer using something like:

create function bytea_to_integer(bytea)
returns integer strict
language sql as $$
  select
     (get_byte($1,0)*1::integer<<0*8)
    +(get_byte($1,1)*1::integer<<1*8)
    +(get_byte($1,2)*1::integer<<2*8)
    +(get_byte($1,3)*1::integer<<3*8);
$$;

这篇关于PostgreSQL中有任何哈希函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆