Jump to content

Recommended Posts

Hello,

 

In my db I have three tables. One holds data for all the people currently in the system (call it "persons"), another holds all possible interests ("interests"), and the third lists the actual interests people in the system have ("person_interests"). Each row in person_interests contains person_id and interest_id.

 

So here's my issue. One of the possible interests is "All." Rather than give this its own entry, I want to grab everything from interests and insert it into person_interests along with the person_id of whoever selects this choice. For example:

 

persons:

---------

id  | name

---------

42  Bob

---------

 

interests:

---------

id | name

---------

1    Hiking

2    Boating

3    Biking

---------

 

If Bob selects "All", then person_interests will have

 

----------------------

person_id | interest_id

----------------------

42            1

42            2

42            3

 

Is there a way to do this with one query? Thanks.

Hi,

After some googling I found this to work, however, I did not thoroughly test it.  (Source: Comment #2)

 

INSERT INTO person_interests( person_id, interest_id )
SELECT p.person_id, i.interest_id
FROM persons p, interests i
WHERE p.person_id = 42

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.