Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U2waq-0008U3-3z for pgsql-sql@arkaria.postgresql.org; Wed, 06 Feb 2013 04:26:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U2wap-00047E-3w for pgsql-sql@arkaria.postgresql.org; Wed, 06 Feb 2013 04:26:43 +0000 Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U2wai-00040G-1G for pgsql-sql@postgresql.org; Wed, 06 Feb 2013 04:26:36 +0000 Received: from plane.gmane.org ([80.91.229.3]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U2wac-0003y8-GI for pgsql-sql@postgresql.org; Wed, 06 Feb 2013 04:26:35 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1U2wao-0001Lt-0m for pgsql-sql@postgresql.org; Wed, 06 Feb 2013 05:26:42 +0100 Received: from ip-118-90-72-2.xdsl.xnet.co.nz ([118.90.72.2]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 06 Feb 2013 05:26:42 +0100 Received: from jasen by ip-118-90-72-2.xdsl.xnet.co.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 06 Feb 2013 05:26:42 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Jasen Betts Subject: Re: Setting a default value for a select statement without results Date: 6 Feb 2013 04:07:37 GMT Organization: Dis (not Dat) Organisation Lines: 28 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ip-118-90-72-2.xdsl.xnet.co.nz X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Pg-Spam-Score: -1.9 (-) 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 On 2013-02-06, JORGE MALDONADO wrote: > --f46d0401fb2fcb805e04d50354b1 > Content-Type: text/plain; charset=ISO-8859-1 > > I have an UPDATE query with the following general structure: > > UPDATE table1 SET (SELECT field FROM table2 WHERE conditions ORDER BY > order_field LIMIT 1) assuming you mean this, and you are happy with its performance. UPDATE table1 SET field1 = (SELECT field FROM table2 WHERE conditions ORDER BY order_field LIMIT 1) > Is it possible to assign a default value in case no results are returned by > the SELECT statement? use coalesce. UPDATE table1 SET field1 = coalesce( (SELECT field FROM table2 WHERE conditions ORDER BY order_field LIMIT 1) , default_value ) -- ⚂⚃ 100% natural -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql