0

I have a csv with a list containing certain ids that are useful to me. I wanted to make query from a table (lets call it friends) to get all the id's that are "friends" to the ids contained in the csv. I know that you can query

SELECT id,friend_id 
FROM friends 
WHERE id IN ('id1', 'id2', etc) 

but the list is 300 ids and I don't want to type them all manually. is there a way to search trough a file after IN( )?

1
  • 3
    Import the .CSV into a (TEMP) table, and join with that table (or: use EXISTS() or IN() on that table.) Commented Jul 7, 2018 at 19:41

1 Answer 1

1

If my database permitted at least 300 items (and I think postgresql does) in an "IN" list, then I would use a text editor to reformat the list with single quotes and commas, and then copy and paste. Many text editors allow you to search for patterns/regular expressions, so you can do things like insert text at the beginning or end of a line, to turn it from a plain list of numbers into valid SQL. One can also format a list into a sequence of single row "insert" SQL statements.

This is not a completely general solution, and may have pitfalls if you are dealing with data more complex than simple integer IDs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.