[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

INSERT multiple rows with a subselect? (PostgreSQL)

Posted on 2002-03-22
3
Medium Priority
?
1,779 Views
Last Modified: 2010-08-05
Ok, I am not sure if it works like this, but it's worth a try.

I have three tables, a hypothetical example with the same relationships would be:

books
-----
book_id
author
whatever

pages
-----
page_id
book_id (referring to a book from the table above)
description
whatever

notes
-----
page_id
comment
user

what I need, is the quickest way, given a book_id a user name and a comment to add a row to the notes table for each page associated with that book_id, with the same comment and user (I know it doesn't make much sense in this example, just trust me, it's what I need)

to help visualize it (let's say we are doing this for book 3, the comment is "good book" and the user name is 'billg'):I

NSERT INTO notes VALUES ((SELECT page_id FROM books WHERE book_id = 3),'good book', 'billg')

now this obviously doesn't work, but if it did, it would create as many rows in notes as there are pages with book_id = 3  and each one would have the same user and comment.

Is there a way to do this in one statement?  (I am hopeful, but doubtful)

Oh, as the title says, I am using postgres

thanks,
Dmitri
0
Comment
Question by:_D_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

Accepted Solution

by:
DrSQL earned 800 total points
ID: 6888721
 D  ,
   You want an insert select:

insert into notes (page_id,comment,user)
  select pageid,'good book','billg' from pages
    where book_id=3;

Good luck!
0
 

Author Comment

by:_D_
ID: 6889151
yep, this is definitely from the "bleeding obvious" department - thanks.
0
 

Expert Comment

by:cyiamxtc
ID: 15072864
You can also do it like this as well....(Oracle 8i):

insert into CR_TNW
(select * from CR_TNWDD);


this is selecting across a link too:

insert into CR_TNW
(select * from PPD.CR_TNW@TRMSP);

0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question