hstore数据类型

来源:互联网 发布:券商工资知乎 编辑:程序博客网 时间:2024/06/06 11:00

hstore数据类型作为扩展模块,用来存储具有多个属性值的数据,一种Postgres内置的键值对结构的数据类型

1开启hstore

Create extension hstore

2,创建一个hstore字段:在创建表是只要指定这个字段的数据类型为hstore就可以了:

CREATE TABLE products (

  id serial PRIMARY KEY,

  name varchar,

  attributes hstore

);

3,插入数据:把字段的值用单引号包含一来, 不同的就是要知道创建字典的一些额外的结构:

INSERT INTO products (name, attributes) VALUES (

 'Geek Love: A Novel',

 'author    => "Katherine Dunn",

  pages     => 368,

  category  => fiction'

 );

4,读取数据:

SELECT name, attributes->'pages' as pages

FROM products

WHERE attributes->'pages'= '368'

结果:

 

几种方法获取hstore的值:

方式1>

    SELECT  字段名->'xxxx' FROM tablename;

方式2>

    SELECT 字段名 FROM tablename

    如下操作可以将hstore类型转php数组:

    $xxx = json_decode('{'.str_replace('=>',':',$xxx['字段名']).'}',true); 

 方式3>

    explode() 也可以将hstore类型转变成php数组


0 0
原创粉丝点击