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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:09:12+00:00 2026-05-25T20:09:12+00:00

I am trying to load attributes from two tables to one table. I have

  • 0

I am trying to load attributes from two tables to one table.

I have a Location table:

Id  City       State     Country
===============================
1   New York   New York  USA
2   Portland   Oregon    USA
3   Tokyo      Honshu    Japan

And a User table:

Id  First_Name  Last_Name  Hometown_City  Hometown_State  Hometown_Country
===========================================================================
1   Brett       Burr       New York       New York        USA
2   Bucky       Beaver     Portland       Oregon          USA
3   Ranma       Saotome    Tokyo          Honshu          Japan

I’m creating a new table for users, which instead of containing the separate Hometown fields, has a Hometown_Id foreign key.

Something like:

Id  First_Name  Last_Name  Hometown_Id
========================================
1   Brett       Burr       1
2   Bucky       Beaver     2
3   Ranma       Saotome    3

However, I’m having a problem with the syntax when inserting the data into the new Users table.

I can insert the non-location based fields easily with a command like:

INSERT INTO newusers (Id, First_Name, Last_Name) 
SELECT DISTINCT Id, First_Name, Last_Name 
FROM users

However, I’m not sure of the correct syntax to then add the values from the location table

  • 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-25T20:09:13+00:00Added an answer on May 25, 2026 at 8:09 pm

    Assuming that you have the appropriate constraints, and that the Location table contains unique entries, the following should work:

    INSERT INTO newusers (Id, First_Name, Last_Name, Hometown_Id)
    SELECT users.Id, users.First_Name, users.Last_Name, locations.Id
    FROM users
    INNER JOIN locations
    ON locations.City = users.Hometown_City
    AND locations.State = users.Hometown_State
    AND locations.Country = users.Hometown_Country
    

    If you have more than one location needed (such as a ‘current residence’ entry), alias and join to the locations table (from users) a second time.

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

Sidebar

Related Questions

I am trying to load data from one table to another. But I only
I am am trying to load a SQL table from a flat file. The
I have an object returned from $xml = simplexml_load_file($file). I'm trying to access 'location-id'
I am trying to create a new XML file from an exisiting one using
I am trying this in my Form Load Event cmdCancel.Attributes.Add(onClick, document.forms[0].reset();return false;) but it
I have a problem with out of memory when I'm trying load a few
When trying to load Microsoft.Xna.Framework.dll from any project, it throws a FileNotFoundException. The specified
I am trying to load UIImage object from NSData , and the sample code
I have an XML file containing seed data that I'm trying to load into
What I am trying to do is load in objects from an XML save

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.