Doc patch that adds an example of a correllated UPDATE.
David Fetter
This commit is contained in:
parent
807cb000f6
commit
cd5d464e9f
@ -1,5 +1,5 @@
|
|||||||
<!--
|
<!--
|
||||||
$PostgreSQL: pgsql/doc/src/sgml/ref/update.sgml,v 1.33 2005/10/12 23:19:22 tgl Exp $
|
$PostgreSQL: pgsql/doc/src/sgml/ref/update.sgml,v 1.34 2006/01/19 23:09:42 momjian Exp $
|
||||||
PostgreSQL documentation
|
PostgreSQL documentation
|
||||||
-->
|
-->
|
||||||
|
|
||||||
@ -205,14 +205,32 @@ UPDATE employees SET sales_count = sales_count + 1 FROM accounts
|
|||||||
WHERE accounts.name = 'Acme Corporation'
|
WHERE accounts.name = 'Acme Corporation'
|
||||||
AND employees.id = accounts.sales_person;
|
AND employees.id = accounts.sales_person;
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
Perform the same operation, using a sub-select in the
|
Perform the same operation, using a sub-select in the
|
||||||
<literal>WHERE</literal> clause:
|
<literal>WHERE</literal> clause:
|
||||||
<programlisting>
|
<programlisting>
|
||||||
UPDATE employees SET sales_count = sales_count + 1 WHERE id =
|
UPDATE employees SET sales_count = sales_count + 1 WHERE id =
|
||||||
(SELECT sales_person FROM accounts WHERE name = 'Acme Corporation');
|
(SELECT sales_person FROM accounts WHERE name = 'Acme Corporation');
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
Now that all the papers are signed, update the most recently closed
|
||||||
|
deal of the travelling salesperson who closed the Rocket Powered
|
||||||
|
Skates deal with the Acme Corporation.
|
||||||
|
<programlisting>
|
||||||
|
UPDATE employees SET last_closed_deal = deal.id
|
||||||
|
FROM accounts JOIN deals ON (account.id = deal.account_id)
|
||||||
|
WHERE deal.employee_id = employees.id
|
||||||
|
AND deal.name = 'Rocket Powered Skates'
|
||||||
|
AND accounts.name = 'Acme Corporation'
|
||||||
|
ORDER BY deal.signed_date DESC LIMIT 1;
|
||||||
|
</programlisting>
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
Attempt to insert a new stock item along with the quantity of stock. If
|
Attempt to insert a new stock item along with the quantity of stock. If
|
||||||
the item already exists, instead update the stock count of the existing
|
the item already exists, instead update the stock count of the existing
|
||||||
item. To do this without failing the entire transaction, use savepoints.
|
item. To do this without failing the entire transaction, use savepoints.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user