Search code examples
sqlpostgresql

Doing a WHERE IN on multiple columns in Postgresql


I have a table 'answers' with an indexed 'problem_id' integer column, a 'times_chosen' integer column, and an 'option' column that's a varchar. Currently the only values for the 'option' column are 'A', 'B', 'C' and 'D', though those may expand later on. I want to increment by one the 'times_chosen' values of many (50-100) answers, when I know the problem_id and option of each of them.

So I need a query that's something like:

UPDATE answers
SET times_chosen = times_chosen + 1
WHERE (problem_id, option) IN ((4509, 'B'), (622, 'C'), (1066, 'D'), (4059, 'A'), (4740, 'A')...)

Is this possible?

Edit to add: Turns out that it is, using the exact syntax that I invented!


Solution

  • It should, at least I've done it before in other SQLs.

    Have you tried it? You can test it with SET times_chosen = times_chosen