?
Solved

SQL Query

Posted on 2013-11-08
3
Medium Priority
?
225 Views
Last Modified: 2013-11-11
I want to write a query that selects multiple columns from one table and 1 column from another table.  Both tables have the same primary key.  

Also, Im not sure if it makes a difference but, the 1 column I want to select from the second table also happens to be the foreign key on the second table (it doesnt exist on the first table).


Can you give me simple syntax for this query?
0
Comment
Question by:tike55
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 668 total points
ID: 39635095
It would be easier if you posted the structure of the two tables fully but basically it would be something like :

Select a.column1, a.column2, a.column3, b.column2
From table1 a, table2 b
Where a.column1 = b.column1

The above select assumes that column1 in each table is the primary key that exists in both tables.
The 'a' and 'b' prefixes refer to the aliases used for the tables as specified in the from clause.
The which clause provides the join between the two tables on the shared primary key.
The required field in Table 2 is referenced in the Select list as b.Column2 which is an alias for table2.Column2
0
 
LVL 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 664 total points
ID: 39635106
While jacko72's code will work, in SQL Server it is more common to use the join syntax:

Select a.column1, a.column2, a.column3, b.column2
From table1 a inner join table2 b on a.column1 = b.column1
0
 
LVL 32

Accepted Solution

by:
awking00 earned 668 total points
ID: 39635902
select a.col_x, a.col_y, a.col_z, b.fk
from tablea a inner join tableb b
on a.pk = b.pk
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question