How to do this in SQL?
How to do this in SQL?
От:
Chris Bitmead <chris.bitmead@bigfoot.com>
Дата:
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?
--
Chris Bitmead
http://www.bigfoot.com/~chris.bitmead
mailto:chris.bitmead@bigfoot.com
Re: [GENERAL] How to do this in SQL?
От:
Stuart Rison <stuart@ludwig.ucl.ac.uk>
Дата:
>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 |
+-------------------------+--------------------------------------+