pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
UPDATE query with variable number of OR conditions in WHERE
3+ messages / 3 participants
[nested] [flat]

* UPDATE query with variable number of OR conditions in WHERE
@ 2013-03-14 22:25 JORGE MALDONADO <jorgemal1960@gmail.com>
  2013-03-14 23:58 ` Re: UPDATE query with variable number of OR conditions in WHERE Ben Morrow <ben@morrow.me.uk>
  0 siblings, 1 reply; 3+ messages in thread

From: JORGE MALDONADO @ 2013-03-14 22:25 UTC (permalink / raw)
  To: pgsql-sql

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?

Respectfully,
Jorge Maldonado

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: UPDATE query with variable number of OR conditions in WHERE
  2013-03-14 22:25 UPDATE query with variable number of OR conditions in WHERE JORGE MALDONADO <jorgemal1960@gmail.com>
@ 2013-03-14 23:58 ` Ben Morrow <ben@morrow.me.uk>
  2013-03-15 03:15   ` Re: UPDATE query with variable number of OR conditions in WHERE Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Ben Morrow @ 2013-03-14 23:58 UTC (permalink / raw)
  To: jorgemal1960@gmail.com; pgsql-sql

Quoth jorgemal1960@gmail.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



-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: UPDATE query with variable number of OR conditions in WHERE
  2013-03-14 22:25 UPDATE query with variable number of OR conditions in WHERE JORGE MALDONADO <jorgemal1960@gmail.com>
  2013-03-14 23:58 ` Re: UPDATE query with variable number of OR conditions in WHERE Ben Morrow <ben@morrow.me.uk>
@ 2013-03-15 03:15   ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2013-03-15 03:15 UTC (permalink / raw)
  To: Ben Morrow <ben@morrow.me.uk>; +Cc: jorgemal1960@gmail.com; pgsql-sql

Ben Morrow <ben@morrow.me.uk> writes:
> Quoth jorgemal1960@gmail.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, ...);

IN is definitely better style than a long chain of ORs.  Another
possibility is to use = ANY(ARRAY):

    UPDATE table1
    SET field1 = "some value"
    WHERE field2 = ANY (ARRAY[value_1, value_2, ...]);

This is not better than IN as-is (in particular, IN is SQL-standard and
this is not), but it opens the door to treating the array of values as a
single parameter:

    UPDATE table1
    SET field1 = "some value"
    WHERE field2 = ANY ($1::int[]);

(or text[], etc).  Now you can build the array client-side and not need
a new statement for each different number of comparison values.  If
you're not into prepared statements, this may not excite you, but some
people find it to be a big deal.

			regards, tom lane


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2013-03-15 03:15 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-03-14 22:25 UPDATE query with variable number of OR conditions in WHERE JORGE MALDONADO <jorgemal1960@gmail.com>
2013-03-14 23:58 ` Ben Morrow <ben@morrow.me.uk>
2013-03-15 03:15   ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox