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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:20:51+00:00 2026-05-25T16:20:51+00:00

I have two database tables, one in MYSQL and one in MSSQL. Both have

  • 0

I have two database tables, one in MYSQL and one in MSSQL. Both have similar data, and one is based on data from another. They’re in two different databases because one is a remote system that is administered and the local system is a Drupal installation which I’m using to show the data in a more friendly manner through a custom module.

For example, I have a table of this sort of structure in MSSQL:

ID | Title | Description | Other fields I don't care about

And based on pulling data from this table I generate a table in MYSQL:

local_id | remote_id | title | description

When the module is initialized, it goes out and does a select from the MSSQL table and generates records and populates the local database. Remote_id is the ID field in the MSSQL database so we can reference the two records together.

I need to sync up this data, deleting records locally which no longer exist on the remote table and creating new records which do not exist locally, and also update all rows information.

Problem is, that sort of requires at least 2 different transactions with possible by-row transactions as well. Example:

To sync local to Remote and remove non-existent remote records:

Select remote_id from local_table;
  For Each remote_id ( select ID, title, description FROM remote_table where ID = remote_id )
    If record exists
      UPDATE local_table WHERE remote_id = row_id
    Else
      DELETE FROM local_table where remote_id = row_id

Then we need at least one other transaction to get new records (I could update here too if I didn’t do it in the previous loop):

Select ID, title, description from remote_table;
  For each ID ( Select remote_id from local_table )
    If does not exist
      INSERT INTO local_table (VALUES)

So that’s a lot of db activity. It would be easier if the tables were of the same type but as it is that’s the only way I know how to do it. Is there a better way? Could I just pull both result sets into an associative array and compare that way and only do the transactions necessary to remove and create? I’m unsure.

  • 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-25T16:20:51+00:00Added an answer on May 25, 2026 at 4:20 pm

    there are a lot of ways to do this based on the system you house.
    The first assumption i am making is that you have 2 databases and you want to sync data between these 2
    that is MSSQL db must pull data from MySQL and vice versa

    Your approach of using associative arrays is good but what if there are 100 columns in the table? ( in your case it is not but the approach is not future proof)
    So to update 1 row you need to make “n” column comparisons if there are 100 rows, then there will be 100*n comparisons

    Have a look at MySQL REPLACE, INSERT INTO .. ON DUPLICATE KEY clauses that might help you – i dont know if there are such clauses in MSSQL

    You can do other things like – have a “last_updated” column in each database table – whenever a column in the table gets updated, this time-stamp field must be updated

    This way you can tell if a row in either database table was updated ( by comparing it to your old timestamp value) and only update those rows

    logic would be in these lines

    to sync local to remote 
    foreach localrow 
      get the common_id of the row 
      get the timestamp of the row 
      check if a row with this common_id exists in the remote table 
      if no then insert 
      if yes then 
        compare timestamps between local and remote row 
        if local row timestamp > remote row timestamp then update remote row 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two MySQL database tables: one containing a list of championships and another
I have two different database in different machine.Both the have same tables and i
I have two completely different MySQL databases and they both have their own user
I have two related tables in my database: Page and Tag. One Page can
I have a two same tables.Database mysql. How can I compare two tables? Table1,Table2
I am using Mysql. And I have two tables which are one-to-one related with
I've got two tables (MySQL), one with a list of categories and another with
i have three tables, i want to select all data from one of the
I have two database tables with the following structure: actions: action_id int(11) primary key
I have a database two tables and a linking table that I need a

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.