Start New Discussion within our Software Development Community

I have two tables, table_1, and table_2.

I am trying to query both tables, I want every column that is in table_1, and only one column from table_2.
Each table has an AccountID column, so they can relate to eachother.

I am trying to get a query result that gives me all the info from table_1 for each account and also table_2's column that is associated with each account. \

EXAMPLE: table1 has accountID,firstName,lastName
table2 has accountID, middleName.
*I want it to return their firstname,lastname and middle name per account.

Any help or tips would greatly be appreciated. Thanks.

So you basically need to use some form of join. When you say..."gives me all the info from table_1 for each account and also table_2's column that is associated with each account.", you need to be more specific.. If you want to show every row in table1 and match that with what is in table2 then you need a left join. If there is no match in table2, the left join will just return a "null" value for that accountID.

So for example...

SELECT table1.accountID, table1.firstName, table2.middleName, table1.lastName 
FROM table1 
LEFT JOIN table2 
ON table1.accountID = table2.accountID

More info on SQL Left JOIN

******
However, say you only want the results when there is a match in both tables... Then you would construct an inner join as follows..

SELECT table1.accountID, table1.firstName, table2.middleName, table1.lastName 
FROM table1 
INNER JOIN table2 
ON table1.accountID = table2.accountID

More info on SQL Inner JOIN

This article has been dead for over six months. Start a new discussion instead.