Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9196997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:58:29+00:00 2026-06-17T21:58:29+00:00

I have tables to organize tags.. 3 tables for articles and article tags: article_tags

  • 0

I have tables to organize tags..

3 tables for articles and article tags:

article_tags
---
tag_id *
tag_name

articles
---
article_id *
article_name 

individual_article_tags
----
article_id *
tag_id *

AND 3 separate tables for images and images tags

image_tags
---
tag_id *
tag_name

images
---
image_id *
image_name 

individual_image_tags
----
image_id *
tag_id *

I want the articles and images tables to reference Only 1 tag table instead of 2 tag tables like so:

tags
---
tag_id *
tag_name

articles
---
article_id *
article_name 

individual_article_tags
----
article_id *
tag_id *

images
---
image_id *
image_name 

individual_image_tags
----
image_id *
tag_id *

The problem is that the tags have different IDs and I don’t know how to merge them..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:58:30+00:00Added an answer on June 17, 2026 at 9:58 pm

    First, create an new tags_unified table with its own auto_increment id (which will differ from either of the other existing tables) by doing INSERT INTO ...SELECT with a UNION. The result will be a distinct set of all tags from both tables.

    CREATE TABLE tags_unified (
      tags_id INT PRIMARY KEY AUTO_INCREMENT,
      tag_name VARCHAR(64)
    );
    
    /* Load up all the tags with new ids */
    INSERT INTO tags_unified (tag_name)
      SELECT tag_name FROM article_tags
      UNION
      SELECT tag_name FROM image_tags
    

    Then UPDATE all the individual_image_tags and inidividual_article_tags with a JOIN query to get the new ids.

    UPDATE
      individual_image_tags
      JOIN image_tags ON individual_image_tags.tag_id = image_tags.tag_id
      /* LEFT JOIN the old tags to the new tags by tag_name */
      LEFT JOIN tags_unified ON image_tags.tag_name = tags_unified.tag_name
    SET individual_image_tags.tag_id = tags_unified.tag_id
    /* And update those which have a match in the new tags table */
    WHERE tags_unified.tag_id IS NOT NULL
    
    UPDATE
      individual_article_tags
      JOIN article_tags ON individual_article_tags.tag_id = article_tags.tag_id
      LEFT JOIN tags_unified ON article_tags.tag_name = tags_unified.tag_name
    SET individual_article_tags.tag_id = tags_unified.tag_id
    WHERE tags_unified.tag_id IS NOT NULL
    

    Before running the UPDATE statements, re-form them as SELECT statements to verify the results.

    SELECT
      individual_image_tags.*, 
      tags_unified.*
    FROM
      individual_image_tags
      JOIN image_tags ON individual_image_tags.tag_id = image_tags.tag_id
      LEFT JOIN tags_unified ON image_tags.tag_name = tags_unified.tag_name
    WHERE tags_unified.tag_id IS NOT NULL
    
    SELECT
     individual_article_tags.*, 
     tags_unified.*
    FROM
      individual_article_tags
      JOIN article_tags ON individual_article_tags.tag_id = article_tags.tag_id
      LEFT JOIN tags_unified ON article_tags.tag_name = tags_unified.tag_name
    WHERE tags_unified.tag_id IS NOT NULL
    

    Edit:

    The LEFT JOIN and the WHERE clause should not actually be necessary, because the should be a match for every existing tag in the tags_unified table.

    Update after comments:

    It may be easier to create temporary tables and repopulate the original link tables from those than to drop and re-add unique constraints or composite keys. Use CREATE TEMPORARY TABLE AS SELECT.... Then delete all rows from the original table and use INSERT INTO ... SELECT to re-fill it from the temp table.

    CREATE TEMPORARY TABLE temp_individual_article_tags AS
        SELECT
         individual_article_tags.article_id 
         tags_unified.tag_id
        FROM
          individual_article_tags
          JOIN article_tags ON individual_article_tags.tag_id = article_tags.tag_id
          LEFT JOIN tags_unified ON article_tags.tag_name = tags_unified.tag_name
        WHERE tags_unified.tag_id IS NOT NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tables A and B with identity PKs ida and idb : ex
I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have tables linked by FK, I query on the first table using entity
I have tables named news and tags_news. Given a news id I have to
I have tables Product ( ID, CategoryID, ... ) Category ( ID, Name, ..
In the database we have tables called Sites, Organisations, Person, Department have the same
I used to have tables before to display the content and people here advised
hi i have tables like this: Persons: person_id, name then i have many langauge
Our client's request is to have tables in PDF with rounded corner. I only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.