Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a Django app which uses a Postgres database. I am creating a temp table by doing the following:

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

Notice that the last column (pub_tags) of temp_table contains an array of strings.

For reference, my next line of code inserts data from existing tables into the temp table, and works fine.

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

For the last step, I'd like to get the pub_titles from the temp_table where, in the pub_tags column, there is a match to a string that I am entering.

For example, I'd like to get all the pub_titles where the pub_tag array contains the string "men." I'd imagine the syntax would be something like:

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

Which is not correct and throws a syntax error, but hopefully describes what I am trying to do. I'm just not sure how to indicate that pub_tags is an array in this context.

I have been referred to some postgres docs, for example:

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

but no matter what I try I can't get anything to work here.

share|improve this question
    
+1 for using PostgreSQL! – Nicholas Kreidberg Jun 12 '13 at 19:44
    
"throws a syntax error". Always show the exact error message and preferably the PostgreSQL version. – Craig Ringer Jun 13 '13 at 0:09
up vote 4 down vote accepted

from postgres documentation it looks like the syntax might be

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

share|improve this answer
    
perfect. thank you. – user1697845 Jun 12 '13 at 18:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.