Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UGL7C-00013Y-Gb for pgsql-sql@arkaria.postgresql.org; Fri, 15 Mar 2013 03:15:30 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UGL7C-0005Zd-1L for pgsql-sql@arkaria.postgresql.org; Fri, 15 Mar 2013 03:15:30 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UGL7B-0005ZX-2L for pgsql-sql@postgresql.org; Fri, 15 Mar 2013 03:15:29 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UGL79-0005Wf-Fr for pgsql-sql@postgresql.org; Fri, 15 Mar 2013 03:15:28 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id r2F3FHXj019956; Thu, 14 Mar 2013 23:15:17 -0400 (EDT) From: Tom Lane To: Ben Morrow cc: jorgemal1960@gmail.com, pgsql-sql@postgresql.org Subject: Re: UPDATE query with variable number of OR conditions in WHERE In-reply-to: <20130314235807.GA81164@anubis.morrow.me.uk> References: <20130314235807.GA81164@anubis.morrow.me.uk> Comments: In-reply-to Ben Morrow message dated "Thu, 14 Mar 2013 23:58:11 -0000" Date: Thu, 14 Mar 2013 23:15:17 -0400 Message-ID: <19955.1363317317@sss.pgh.pa.us> X-Pg-Spam-Score: -4.3 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Ben Morrow 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