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 4103770
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:00:06+00:00 2026-05-20T21:00:06+00:00

Say I have two tables: articles categories There’s a many to many table that

  • 0

Say I have two tables:

articles
categories

There’s a many to many table that connects them.

CREATE TABLE cat2art (
  article_id INT,
  category_id INT
);

For a specific article, I have a ‘new list’ of category id’s, and we need to update the cat2art table with these.

Some categories got removed, some got added and some stayed where they were. What is the most effective way to update this table?

I could naively delete all records with the specified article_id and simply add them again. However, if I were to record a date in that same table that tracks when an article was linked to a category, that information will now be destroyed.

I’m looking for a great pattern that easily solves this issue. This question is specifically for PHP and MySQL, but answers in other languages are also fine provided they are applicable to PHP+MySQL as well.

  • 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-05-20T21:00:06+00:00Added an answer on May 20, 2026 at 9:00 pm

    Other systems support MERGE statement which would do exactly what you want.

    However, in MySQL, you would need at least two queries (it cannot delete and insert/update in a single statement):

    DELETE
    FROM    cat2art
    WHERE   art_id = $art_id
            AND cat_id NOT IN ($new_cat_1, $new_cat_2, …);
    
    INSERT
    IGNORE
    INTO    cat2art
    VALUES
    ($art_id, $new_cat_1),
    ($art_id, $new_cat_2),
    …;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two tables, user and comment . They have table definitions that
Let say I have two tables with a many-to-many relationship (i.e. there is a
Let's say I have two tables in my database. TABLE:Categories ID|CategoryName 01|CategoryA 02|CategoryB 03|CategoryC
Say I have two tables: create table parent ( id number not null, constraint
Say I have two tables: create table parent ( number not null, constraint parent_pk
Lets say I have two tables - Cat and Cat owner that are linked
Let's say I have two tables that look like this: TH TH TH TH
Here i need help with joins. I have two tables say articles and users.
I have two tables. Lets say they look like this Table Sports: Column 1:
Quick question that requires a long explanation.. Say I have two tables - one

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.