sql - 如何从任何 hstore 值中选择一行?

标签 sql postgresql hstore

我在 PostgreSQL (9.5) 数据库中有一个表 Content,其中包含 title 列。 title 列是一个 hstore。这是一个 hstore,因为 title 被翻译成不同的语言。例如:

example=# SELECT * FROM contents;
 id |                    title                    |                    content                     |         created_at         |         updated_at         
----+---------------------------------------------+------------------------------------------------+----------------------------+----------------------------
  1 | "de"=>"Beispielseite", "en"=>"Example page" | "de"=>"Beispielinhalt", "en"=>"Example conten" | 2016-07-17 09:20:23.159248 | 2016-07-17 09:20:23.159248
    (1 row)

我的问题是,如何选择 title 包含 Example pagecontent

SELECT * FROM contents WHERE title = 'Example page';

不幸的是,这个查询不起作用。

example=# SELECT * FROM contents WHERE title = 'Example page';
ERROR:  Syntax error near 'p' at position 8
LINE 1: SELECT * FROM contents WHERE title = 'Example page';

最佳答案

avals() 函数返回 hstore 列中所有值的数组。然后,您可以使用 any 对该数组匹配您的值:

select *
from contents 
where 'Example page' = any(avals(title))

关于sql - 如何从任何 hstore 值中选择一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38422166/

相关文章:

添加 PostgresStORM 包时,快速完美的项目构建失败

sql - Postgres : how to View an hstore Column as a Table

sql - 为什么此 TSql 返回空白或 null 值?

sql - 使用 IF/ELSE 语句在 SELECT 上触发 PostgreSQL UPDATE

sql - Django限制分组查询的方法

postgresql - 编写扩展 postgresql

ruby-on-rails - 如何为 Ruby on Rails fixtures YAML 文件中的 HStore 字段设置默认值?

php - 向表中添加额外的列

mysql - SQL 在我的用户表上每个季度+年的增长

postgresql - 在 PostgreSQL 中将分钟添加到 CURRENT_TIMESTAMP