[prev in list] [next in list] [prev in thread] [next in thread] 

List:       postgresql-general
Subject:    Re: [GENERAL] How to do this in SQL?
From:       Stuart Rison <stuart () ludwig ! ucl ! ac ! uk>
Date:       1999-03-30 15:50:12
[Download RAW message or body]

>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?
>

try \h insert in psql for a description of the correct syntax.

Your query becomes:

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

Stuart.


+-------------------------+--------------------------------------+
| Stuart Rison            | Ludwig Institute for Cancer Research |
+-------------------------+ 91 Riding House Street               |
| Tel. (0171) 878 4041    | London, W1P 8BT, UNITED KINGDOM.     |
| Fax. (0171) 878 4040    | stuart@ludwig.ucl.ac.uk              |
+-------------------------+--------------------------------------+

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic