Skip navigation links

MySQL Forums :: Transactions :: SELECT INTO from two tables?


Advanced Search

SELECT INTO from two tables?
Posted by: Christopher Swearingen ()
Date: December 08, 2009 02:42PM

We've got an in-house database allows us to group the contacts into lists. We can then create a note and apply it to all the contacts in that list. The tables are basically structured like so:

                   ┌─────────────────────────────────────┐
╔═══════════════╗  │    ╔══════════════════════════╗     │
║  contacts     ║  │    ║  contact_list            ║     │
╠═══════════════╣  │    ╠══════════════════════════╣     │
║  contact_id   ║──┤  ┌─║ contact_list_id          ║──┐  │
║  contact_name ║  │  │ ║ contact_list_description ║  │  │
╚═══════════════╝  │  │ ╚══════════════════════════╝  │  │
                   │  │                               │  │
                   │  │                               │  │
╔═══════════════════════╗   ╔════════════╗    ╔══════════════════════╗
║ contact_list_contacts ║   ║ mass_notes ║    ║ mass_note_recipients ║
╠═══════════════════════╣   ╠════════════╣    ╠══════════════════════╣
║ contact_list_id       ║   ║ note_id    ║────║ note_id              ║
║ contact_id            ║   ║ note_text  ║    ║ contact_id           ║
╚═══════════════════════╝   ╚════════════╝    ╚══════════════════════╝

The following query gives me the exact set of values that I want to insert into mass_note_recipients:
SELECT mass_notes.note_id, contact_list.contact_ID
FROM mass_notes, contact_list
WHERE mass_notes.note_id = [passed in note id]
AND contact_list.contact_list_ID = [passed in list ID]

Currently the system for writing the mass notes is to loop through this set and then add a record in the mass_note_recipients linking table. However this can take a loooooonnngggg time if there are several thousand contacts in a contact list. So I was looking at using a SELECT INTO statement to see if I could take the above result and, with one statement instead of a loop, insert the data into the relational table (the hard-coded values will eventually be filled in by the application itself):

SELECT mass_notes.note_id, contact_list.contact_ID
INTO mass_notes_relationships
FROM mass_notes, contact_list
WHERE mass_notes.note_id = [passed in note id]
AND contact_list.contact_list_ID = [passed in list ID]

This query results in an SQL error 1327 "Undeclared Variable:mass_notes_relationships". So is a SELECT INTO just not possible using two non-related tables like this? I hope my question makes sense.

Options: ReplyQuote


Subject Views Written By Posted
SELECT INTO from two tables? 11126 Christopher Swearingen 12/08/2009 02:42PM
Re: SELECT INTO from two tables? 2852 Christopher Swearingen 12/09/2009 11:25AM
Re: SELECT INTO from two tables? 2596 Christopher Swearingen 12/09/2009 11:38AM
Re: SELECT INTO from two tables? 2279 Peter Brawley 12/10/2009 03:04PM
Re: SELECT INTO from two tables? 3230 Roberto Novakosky 12/18/2009 09:08AM
Re: SELECT INTO from two tables? 2331 Christopher Swearingen 12/18/2009 02:36PM
Re: SELECT INTO from two tables? 2270 Christopher Swearingen 12/18/2009 04:26PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.