如何查询postgres中的HSTORE [] - 9.2及以上版本?

时间:2014-03-20 08:20:36

标签: postgresql ruby-on-rails-4 postgresql-9.1 psql hstore

我正在使用带有Rails 4的postgress-9.3。

我有一个hstore的数据类型数组(HSTORE [])。

请参阅产品表上的常规查询。

SELECT id, specifications FROM "products" limit 10;

 id |                                              specifications                                              
----+----------------------------------------------------------------------------------------------------------
  1 | {"\"1368\"=>\"1\"","\"1368\"=>\"0.5\"", "\"1371\"=>\"male/male\"","\"1370\"=>\"LAN (RJ-45)\"","\"1369\"=>\"LAN (RJ-45)\""}
  2 | {"\"1368\"=>\"0.46\""}
  3 | {"\"1368\"=>\"1.5\""}
  4 | {"\"1368\"=>\"5\""}
  5 | {"\"1368\"=>\"3\""}
  6 | {"\"1368\"=>\"2\"","\"1371\"=>\"male/male\"","\"1370\"=>\"LAN (RJ-45)\"","\"1369\"=>\"LAN (RJ-45)\""}
  7 | {"\"1368\"=>\"0.25\"","\"1371\"=>\"male/male\"","\"1370\"=>\"LAN (RJ-45)\"","\"1369\"=>\"LAN (RJ-45)\""}
  8 | {"\"1368\"=>\"0.25\"","\"1371\"=>\"male/male\"","\"1370\"=>\"LAN (RJ-45)\"","\"1369\"=>\"LAN (RJ-45)\""}
  9 | {"\"1368\"=>\"0.25\"","\"1371\"=>\"male/male\"","\"1370\"=>\"LAN (RJ-45)\"","\"1369\"=>\"LAN (RJ-45)\""}
 10 | {"\"1368\"=>\"0.25\"","\"1371\"=>\"male/male\"","\"1370\"=>\"LAN (RJ-45)\"","\"1369\"=>\"LAN (RJ-45)\""}

我的要求是根据规格值查询产品表,例如:根据1368(HSTORE []中的KEY)小于1的条件列出所有产品(HSTORE []中的VALUE)。

# the result should 

id |                                              specifications                                              
----+----------------------------------------------------------------------------------------------------------
  1 | {"\"1368\"=>\"1\"","\"1368\"=>\"0.5\"", "\"1371\"=>\"male/male\"","\"1370\"=>\"LAN (RJ-45)\"","\"1369\"=>\"LAN (RJ-45)\""}
  2 | {"\"1368\"=>\"0.46\""}
  7 | {"\"1368\"=>\"0.25\"","\"1371\"=>\"male/male\"","\"1370\"=>\"LAN (RJ-45)\"","\"1369\"=>\"LAN (RJ-45)\""}
  8 | {"\"1368\"=>\"0.25\"","\"1371\"=>\"male/male\"","\"1370\"=>\"LAN (RJ-45)\"","\"1369\"=>\"LAN (RJ-45)\""}
  9 | {"\"1368\"=>\"0.25\"","\"1371\"=>\"male/male\"","\"1370\"=>\"LAN (RJ-45)\"","\"1369\"=>\"LAN (RJ-45)\""}
 10 | {"\"1368\"=>\"0.25\"","\"1371\"=>\"male/male\"","\"1370\"=>\"LAN (RJ-45)\"","\"1369\"=>\"LAN (RJ-45)\""}

如何实现?

0 个答案:

没有答案