Re: UPDATE query with variable number of OR conditions in WHERE

From: Ben Morrow <ben(at)morrow(dot)me(dot)uk>
To: jorgemal1960(at)gmail(dot)com, pgsql-sql(at)postgresql(dot)org
Subject: Re: UPDATE query with variable number of OR conditions in WHERE
Date: 2013-03-14 23:58:11
Message-ID: 20130314235807.GA81164@anubis.morrow.me.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Quoth jorgemal1960(at)gmail(dot)com (JORGE MALDONADO):
>
> I am building an UPDATE query at run-time and one of the fields I want to
> include in the WHERE condition may repeat several times, I do not know how
> many.
>
> UPDATE table1
> SET field1 = "some value"
> WHERE (field2 = value_1 OR field2 = value_2 OR .....OR field2 = value_n)
>
> I build such a query using a programming language and, after that, I
> execute it. Is this a good approach to build such a query?

You can use IN for this:

UPDATE table1
SET field1 = "some value"
WHERE field2 IN (value_1, value_2, ...);

Ben

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2013-03-15 03:15:17 Re: UPDATE query with variable number of OR conditions in WHERE
Previous Message JORGE MALDONADO 2013-03-14 22:25:48 UPDATE query with variable number of OR conditions in WHERE