pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: Chris Bitmead <chris.bitmead@bigfoot.com>
To: pgsql-sql@postgreSQL.org
Subject: Re: [GENERAL] How to do this in SQL?
Date: Tue, 30 Mar 1999 16:59:16 +0200
Message-ID: <l0311070fb32695e2d222@[147.233.148.140]> (raw)
In-Reply-To: <3700E388.3E3A77E1@bigfoot.com>

This question's topic is more appropriate to the SQL list than to the
GENERAL list, so I am redirecting it there.

At 16:45 +0200 on 30/03/1999, Chris Bitmead wrote:

>
> Let's say I had a table...
> CREATE TABLE book (
>    author oid,
>    name text );
> and...
> CREATE TABLE author (
>    name text );
>
> and I wanted to create a book pointing to author with name 'Tolstoy'. I
> want to do something like...
>
> INSERT INTO book(name,author) values('War and Peace',
>       (SELECT oid FROM author WHERE name = 'Tolstoy'));
>
> but this doesn't work. What is the correct syntax?

This is not the correct syntax. The proper one is:

INSERT INTO BOOK (name, author)
SELECT 'War and Peace', oid
FROM author
WHERE name = 'Tolstoy';

This syntax appears in the documentation for the INSERT command, but most
people disregard it, because they thing the SELECT statement must only
select fields or expressions that involve fields. The truth is that the
SELECT statement can select contstant-valued expressions. Perhaps an
example in this spirit should be added to the documentation.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





view thread (5+ messages)  latest in thread

Message-ID: <l0311070fb32695e2d222@[147.233.148.140]>
Permalink:  ../l0311070fb32695e2d222@%5B147.233.148.140%5D/
Also on:    postgresql.org/message-id/l0311070fb32695e2d222@[147.233.148.140]

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: herouth@oumail.openu.ac.il, chris.bitmead@bigfoot.com
  Subject: Re: [GENERAL] How to do this in SQL?
  In-Reply-To: <l0311070fb32695e2d222@[147.233.148.140]>
  References: <3700E388.3E3A77E1@bigfoot.com> <l0311070fb32695e2d222@[147.233.148.140]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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