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

  • Home
  • SEARCH
  • 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 3878860
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:39:37+00:00 2026-05-19T22:39:37+00:00

I have two tables which have the exact same structure. Both tables can store

  • 0

I have two tables which have the exact same structure. Both tables can store the same data with different primary keys (autoincremented integers). Therefore, there is a third table which lists which two primary keys list the same data. However, there also exist rows which don’t exist in the other. Therefore, a simple join won’t work since you will have two rows with the same primary key but different data. Therefore, is there a way of reassigning primary keys to unused values in the view?

Table1

 ID    name
 1     Adam
 2     Mark
 3     David
 4     Jeremy

Table2

 ID    name
 1     Jessica
 2     Jeremy
 3     David
 4     Mark

Table3

 T1ID  T2ID
 2     4
 3     3
 4     2

I am looking for a result table like the following:

Result

 ID    name
 1     Adam
 2     Mark
 3     David
 4     Jeremy
 5     Jessica

The real heart of the question is how i can assign the temporary fake id of 5 to Jessica and not just some random number. The rule I want for the ids is that if the row exists in the first table, then use its own id. Otherwise, use the next id that an insert statement would have generated (the column is on autoincrement).

  • 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-19T22:39:38+00:00Added an answer on May 19, 2026 at 10:39 pm

    Answer to edited question

    select id, name from table1
    union all
    select X.offset + row_number() over (order by id), name
    from (select MAX(id) offset from table1) X
    cross join table2
    where not exists (select * from table3 where t2id = table2.id)
    

    The MAX(id) is used to “predict” the next identity that would occur if you merged the data from the 2nd table into the first. If Table3.T2ID exists at all, it means that it is already included in table1.

    Using the test data below

    create table table1 (id int identity, name varchar(10))
    insert table1 select 'Adam' union all
    select 'Mark' union all
    select 'David' union all
    select 'Jeremy'
    
    create table table2 (id int identity, name varchar(10))
    insert table2 select 'Jessica' union all
    select 'Jeremy' union all
    select 'David' union all
    select 'Mark'
    
    create table table3 (t1id int, t2id int)
    insert table3 select 2,4 union all
    select 3,3 union all
    select 4,2
    

    Answer to original question below

    So the 3rd table is the one you want to build (a view instead of a table)?

    select newid=row_number() over (order by pk_id), *
    from
    (
        select a.*
        from tblfirst a
        UNION ALL
        select b.*
        from tblsecond b
    ) X
    

    The data will contain a unique newid value for each record, whether from first or second table. Change pk_id to your primary key column name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables which get updated at almost the exact same time -
I have two different databases that contain two tables with the exact same design.
I have two mysql tables which both have a typeID in common. I am
I have two HTML tables which would ideally be placed side by side on
Hi I want to have two tables each have an INT id column which
I have a two-dimensional array (of Strings) which make up my data table (of
I have two tables, both with start time and end time fields. I need
In a class A I have two scopes, s1 and s2 which both join
i have two mysql tables which are linked together and i need to show
I have a two tables which are linked by a one-to-many relationship. Now I

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.