Solved

SQL Query

Posted on 2013-11-08
3
216 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
3 Comments
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 167 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 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 166 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 167 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

839 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