3

我有一个使用 Postgres 数据库的 Django 应用程序。我正在通过执行以下操作创建一个临时表:

cursor.execute("""CREATE TEMP TABLE temp_table (pub_id INTEGER, pub_title TEXT, pub_tags TEXT[])""")

请注意,temp_table 的最后一列 (pub_tags) 包含一个字符串数组。

作为参考,我的下一行代码将现有表中的数据插入到临时表中,并且工作正常。

cursor.execute("""INSERT INTO temp_table(pub_id, pub_title, pub_tags) SELECT...etc.

最后一步,我想从 temp_table 中获取 pub_titles,在 pub_tags 列中,与我输入的字符串匹配。

例如,我想获取 pub_tag 数组包含字符串“men”的所有 pub_titles。 我想语法会是这样的:

 cursor.execute("""SELECT pub_title FROM temp_table WHERE '%men%' IN (pub_tags)""")

这是不正确的并引发语法错误,但希望能描述我正在尝试做的事情。我只是不确定如何在这种情况下表明 pub_tags 是一个数组。

我被提到了一些 postgres 文档,例如:

http://www.postgresql.org/docs/current/static/functions-array.htmlhttp://www.postgresql.org/docs/current/interactive/functions-comparisons.html#AEN18030

但无论我尝试什么,我都无法在这里工作。

4

1 回答 1

9

从 postgres文档看来,语法可能是

SELECT pub_title FROM temp_table WHERE 'men' = ANY (pub_tags)

于 2013-06-12T18:26:42.403 回答