Note that there are some explanatory texts on larger screens.

plurals
  1. POInnoDB: Copying a number of records along with their tag memberships
    primarykey
    data
    text
    <p><strong>Foreign keys might be appropriate to this problem/solution. However, I have inherited this code and db, which do not use foreign keys, and so it would be difficult to add them. If absolutely necessary I can do it, but I'd prefer not to.</strong></p> <p>Let's pretend that I have a very simple set of tables in an <code>InnoDB</code> database that are used to store a bunch of jokes, each of which belongs to particular group and may have one or more tags associated with it. I am using <code>PHP/MySQLi</code> to do the work. Let's say the tables look like so:</p> <pre><code>GROUPS id (int, primary, auto_inc) | group_name (varchar[64]) ============================================================ 1 knock, knock jokes 2 one-liners JOKES id (int, primary, auto_inc) | group_id (int) | joke_text (varchar[255]) ============================================================================= 1 1 interrupting cow. inte-MOO! TAGS id (int, primary, auto_inc) | tag_text (varchar[255]) ============================================================================= 1 explicit 2 hilarious JOKE_TAGS id (int, primary, auto_inc) | joke_id (int) | tag_id (int) ============================================================================= 1 1 1 </code></pre> <p><em>Even though it makes no sense in the context of these jokes</em>, let's just say that the user has the option to copy the jokes from one group to another. Thanks to users' help on this site, I have figured out that the easiest way to do that would be something like the following:</p> <pre><code>INSERT INTO jokes (group_id,joke_text) SELECT '$dstGroupID', r2.joke_text FROM jokes AS j2 WHERE j2.group_id = '$srcGroupID'; </code></pre> <p>That seems to work just fine. However, I am completely lost as to how I can efficiently copy over the tag memberships. For instance, if I was to copy the <code>jokes</code> from <code>group.id=1</code> to <code>group.id=2</code> (using the sample data shown above), I would want the <code>JOKE_TAGS</code> table to look like so:</p> <pre><code>JOKE_TAGS id (int, primary, auto_inc) | joke_id (int) | tag_id (int) ============================================================================= 1 1 1 2 2 1 </code></pre> <p>For the life of me, I simply cannot figure out a way to do this without throwing away the SQL above and simply iterating through every single <code>joke</code> that is being copied, with the logic looking something like this:</p> <ul> <li>Pull out a joke's information</li> <li>Pull out that joke's tag memberships</li> <li>Insert a new record into <code>JOKES</code> with the pulled out info from above</li> <li>Grab the id of the newly inserted joke</li> <li>Insert a new record into <code>JOKE_TAGS</code>, using the id grabbed above</li> </ul> <p>This is obviously WILDLY inefficient when compared to the 'copying' SQL listed above. If anyone can suggest a more efficient solution, I'd be most appreciative.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload