dcsimg
Results 1 to 2 of 2

Thread: Combining columns from different tables

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2018
    Posts
    76

    Combining columns from different tables

    Is it possible to run a select query on a table (Table A column 1, 2, and 3) and add a column to the result from another table (Table B column 1)?
    It is sort of combination of two tables but the extra column (Table B column 1) does not have the same length as column of table A.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,984

    Re: Combining columns from different tables

    It sounds like you simply need to include a join in your query. Joins are one of the absolute basics of SQL, so there's loads of information around on the subject.
    the extra column (Table B column 1) does not have the same length as column of table A
    It's not completely clear what you mean by that. Are you saying that table B has a different number of records in it than table A? If so, that's no issue. You just have to decide how you want the data joined. Either way, you will specify one or more columns to join on that contain data that relates the two tables. For instance, a Parent table might have a ParentId column as primary key and then a Child table might have a ParentId column as a foreign key. You would then join on ParentId and combine data from records in both tables where the values in that column is the same. That means that if there are multiple Child records with the same ParentId value, the corresponding Parent data will appear in the result set multiple times.

    If you perform an inner join then any Parent records with a ParentId value that doesn't appear in the Child table will be omitted from the result set. If you perform an outer join, every Parent record will be included in the result set and the columns from the Child table will be NULL for those Parent records where the ParentId value doesn't appear in the Child table. It's for you to decide which option is more appropriate for your scenario. Inner joins are far more common but outer joins have their place.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width