Re: [GENERAL] How to do this in SQL?

From: Herouth Maoz <herouth(at)oumail(dot)openu(dot)ac(dot)il>
To: Chris Bitmead <chris(dot)bitmead(at)bigfoot(dot)com>, pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [GENERAL] How to do this in SQL?
Date: 1999-03-30 14:59:16
Message-ID: l0311070fb32695e2d222@[147.233.148.140]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Chris Bitmead 1999-03-30 15:03:37 Re: [GENERAL] How to do this in SQL?
Previous Message Kollar Lajos 1999-03-30 14:49:08 Re: [GENERAL] sorting by date & time in descending order ??

Browse pgsql-sql by date

  From Date Subject
Next Message Chris Bitmead 1999-03-30 15:03:37 Re: [GENERAL] How to do this in SQL?
Previous Message Kollar Lajos 1999-03-30 14:49:08 Re: [GENERAL] sorting by date & time in descending order ??