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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:32:17+00:00 2026-06-01T02:32:17+00:00

How do I merge two tables in MySQL? I’ve looked at several other posts

  • 0

How do I merge two tables in MySQL?
I’ve looked at several other posts on this topic but they don’t go into enough detail for me.

I’m a novice MySQL user, so bear with me I have a primary table and a temp table that look like this:

CREATE TABLE temp_import ( id
int(11) NOT NULL auto_increment,
Name varchar(255) default NULL,
MerchantID int(11) default NULL,
SKU varchar(255) default NULL,
PRIMARY KEY (id) ) ENGINE=MyISAM
AUTO_INCREMENT=765811 DEFAULT
CHARSET=utf8;

I’m inserting data into the temp table like this:

LOAD DATA LOCAL INFILE \'29762.txt\' REPLACE INTO TABLE temp_import FIELDS TERMINATED BY \'|\' OPTIONALLY ENCLOSED BY \'"\' LINES TERMINATED BY \'\n\' (Name, MerchantID, SKU);

The table format of each table is the same, but the data changes over time. Some items the data chances but the SKU remains constant. Some items will no longer be offered and need to be removed from the database.

example:

Current Database Table:

1, dog, 101, dog101
2, cat, 101, cat101
3, chicken, 102, chicken100
4, chicken food, 102, chicken101

New Database Table:

1, dog, 101, dog101
2, cat, 102, cat101
3, chicken, 102, chicken100
5, Frog, 103, frog101

Final Result Should be

1, dog, 101, dog101
2, cat, 102, cat101
3, chicken, 102, chicken100
5, Frog, 103, frog101

Deleted chickenfood, added Frog, updated cat.

Also this needs to be as efficient as possible. I’ll be working with some huge files. And it can be MySQL only code.

http://www.xaprb.com/blog/2006/02/21/flexible-insert-and-update-in-mysql/ I’ve looked over this but it’s over my head, when I try to do this it doesn’t work…

  • 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-01T02:32:18+00:00Added an answer on June 1, 2026 at 2:32 am

    I thought I would post what I eventually used for this. It’s probably not the “BEST” way, and dropping the column is completely optional.

    ALTER TABLE `temp_import`
    ADD `deleteme` tinyint NOT NULL
    DEFAULT 0; -- If you haven't already added a deleteme column 
    
    
    UPDATE `temp_import` SET  `deleteme` = 1; -- Set the delete field 
    
    LOAD DATA LOW_PRIORITY LOCAL INFILE "import.csv" REPLACE INTO TABLE `temp_import`  FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\n"  IGNORE 1 LINES (`id`, `name`, `m_id`, `sku`);
    
    DELETE FROM `temp_import` WHERE  `deleteme` = 1;
    
    ALTER TABLE `tests` DROP COLUMN `deleteme` -- Optional
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two mysql tables and i want to merge the results of these
Trying to merge two tables, but I can't really get my head around it.
I need to merge two tables: Both have a primary key-column date, but with
How can I merge two MySQL tables that have the same structure? The primary
I have a situation where I need to merge two product tables into one
I need to merge two SELECT statements. There are two tables in my database,
I'm trying to merge two main menus together, but am having problems getting the
I'm trying to merge two docx-documents into one docx-document using OpenXML SDK 2.0. The
If I have two tables in mysql that have similar columns... TABLEA id name
What would be the insert SQL statement to merge data from two tables. For

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.