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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:11:47+00:00 2026-05-28T16:11:47+00:00

How to update (change from first select table value second) second_table.first_table_id if first_table.email match

  • 0

How to update (change from first select table value second) second_table.first_table_id if first_table.email match in both select.

If it even possible. With one query!

—————————————– UPDATE —————————————–

EXAMPLE:

I need to update foreign key of second table if email field match in first table. I need to compare two query results with different parent_id (parents are in in same table with children)

         table_1
 -------------------------
| id |  parent_id | email |
 -------------------------
  1     NULL       NULL      
  2     NULL       NULL
  3      1         joe@m.ru
  4      2         bob@f.ly
  5      1         bob@f.ly
  6      2         kira@.us

     table_2
 ----------------
| id |  first_id |
 ----------------
   1       3
   2       4
   3       5
   4       6
  1. I have two parents with ids 1 and 2 and some children (ids: 3,4,5,6).

  2. Also, keep in mind: 1 – old, 2 – new

  3. Task: change foreign key in second table if children email with parent_id = 1 and chilren email with parent_id = 2 match (are the same).

  4. In our example in second table row with id = 3 its foreign key field – first_id has to change from 5 to 4.

  • 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-28T16:11:49+00:00Added an answer on May 28, 2026 at 4:11 pm

    Following might get you started

    UPDATE  Table_2 t2u
    SET     first_id = (
      SELECT  t2.first_id
      FROM    Table_2 t2
              INNER JOIN Table_1 t1 ON t1.id = t2.first_id
              INNER JOIN (
                SELECT  parent_id = MAX(parent_id), email
                FROM    Table_1
                GROUP BY
                        email
              ) t1p ON t1p.email = t1.email
              INNER JOIN Table_1 t1i ON   t1i.email = t1p.email
                                          AND t1i.parent_id = t1p.parent_id
      WHERE   t2u.first_id <> t1i.id)  
    

    Test script (SQL Server)

    ;WITH Table_1 (id, parent_id, email) AS (
      SELECT 1, NULL, NULL      
      UNION ALL SELECT 2, NULL, NULL
      UNION ALL SELECT 3, 1, 'joe@m.ru'
      UNION ALL SELECT 4, 2, 'bob@f.ly'
      UNION ALL SELECT 5, 1, 'bob@f.ly'
      UNION ALL SELECT 6, 2, 'kira@.us'
    )
    , Table_2 (id, first_id) AS (
        SELECT 1, 3
        UNION ALL SELECT 2, 4
        UNION ALL SELECT 3, 5
        UNION ALL SELECT 4, 6
    )
    SELECT  t2.*, t1i.id as [update with]
    FROM    Table_2 t2
            INNER JOIN Table_1 t1 ON t1.id = t2.first_id
            INNER JOIN (
              SELECT  parent_id = MAX(parent_id), email
              FROM    Table_1
              GROUP BY
                      email
            ) t1p ON t1p.email = t1.email
            INNER JOIN Table_1 t1i ON   t1i.email = t1p.email
                                        AND t1i.parent_id = t1p.parent_id
    WHERE   t2.first_id <> t1i.id
    

    Output

    id          first_id    update with 
    ----------- ----------- -----------
    3           5           4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to update two columns in a table. The value of the second
How to change and update the title of the command prompt window from the
How can I write single UPDATE query to change value of COL1 to ‘X’
When I insert a new record in the users table, first check email and
I have 2 dropdownlists, the second (department) cascades from the first (division). I use
I would like to update (change the content) some part of the webpage without
I'm trying to update/change contact ringtone using this code: ContentValues values = new ContentValues();
Update: Last night, I decided that this is just too much work to change
I have a ListView inside of an Update Panel and wanted to change the
I need to be able to update my config file programmatically and change my

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.