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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:39:01+00:00 2026-06-07T18:39:01+00:00

I have a mySQL table with a reference set of data in it. I

  • 0

I have a mySQL table with a reference set of data in it. I have another table with data in it that describes updates or additional entries to this reference data. For various reasons I won’t get into, it’s important that the reference data stay unaltered as specific users come along and create records (hence the second table). The schema between these tables is identical with the exception of the “custom” table having a 2-key composite primary key. One of those columns is shared between the “custom” and reference tables.

The business logic I want to execute in a single SQL statement on these tables is as follows:

If records exist in the “custom” table with the same key value as what’s in the reference table, then take the “custom”. Otherwise, take the reference value.

I thought maybe I could use a RIGHT JOIN, or perhaps even some twist on the UNION operator to control what was coming back. I keep scrambling up the logic of the SQL though. The JOIN is going to fetch info from one table to join with the other, which I’m really not looking for. The UNION will simply remove duplicates between tables….which is close.

Something like:

SELECT * FROM custom UNION SELECT * FROM reference

Except this will simply drop duplicates if all columns are the same. If any is different (which there will be since that’s why the custom entry exists), then the UNION does nothing but munge both tables’ contents together.

Am I going about this all wrong?

Thanks for any insights you can provide.

  • 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-07T18:39:02+00:00Added an answer on June 7, 2026 at 6:39 pm

    There are a few ways to do this. Here is a union that should get what you need, at least according to my interpretation of your situation:

    select KeyCol, Col2, Col3
    from CustomTable
    union all
    select KeyCol, Col2, Col3
    from RefTable
    where KeyCol not in (select KeyCol from CustomTable)
    

    I’m assuming your RefTable and CustomTable have a one-to-zero-or-one relationship, as in, you don’t have multiple Custom records for any Ref (but this will still work either way).

    Update:

    You might also want your extra key column from Custom:

    select KeyCol, Col2, Col3, CustomKey
    from CustomTable
    union all
    select KeyCol, Col2, Col3, null as CustomKey
    from RefTable
    where KeyCol not in (select KeyCol from CustomTable)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql table set up like this id | refference_id | data
I have a MySQL table that looks something like this: +-----+------------+ | id |
I have mysql table that has a column that stores xml as a string.
I have MySQL InnoDB table utf-8 encoded. This table has only id and name
I have a MySQL table called accounts . Within this table is a field
I have a mysql table structure like that: id int primary key name varchar
I have a php page that pulls data from a mysql database based on
I have a mysql table and one of the fields is 'template'. This field
I have a MySQL database holding hierarchical data using the Closure Table method. A
I have Mysql datatbase, and working on servlet: this is my Table schema: CREATE

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.