I want to insert records into the “books” table for every author in the “author” table. In other words, if this represented the author table:
> 1 - "Herman Melville"
> 2 - "Stephen King"
> 3 - "Truman Capote"
…then, the insert would yield these results in the “book” table:
> 'Hello World' - 1
> 'Hello World' - 2
> 'Hello World' - 3
I have the following INSERT statement (it’s actually a crude example, but it demonstrates the objective):
INSERT INTO books (title, author_id) VALUES ('Hello World', SELECT id FROM author);
Unfortunately, this fails and I’m uncertain how to get each of the “id” values from the “author” table.
It’s not clear why you’d want to insert the same entry for each author, but you could use a SELECT as the source for your INSERT, like this:
Edit: for some more info, check the docs for INSERT; as you can see, the source can be either DEFAULT VALUES, VALUES or a query.