k80sg
k80sg

Reputation: 2473

SQL Server + select row and its sub table contents

I have simple select statement like this:

SELECT 
[NAME], 
[AGE], 
[GENDER], 
SELECT [PHOTOS] FROM [USERPHOTOS] WHERE... --Yes, this is wrong but just want to make my intentions clearer. 
FROM PEOPLE WHERE AGE = '20'

The selected people may have multiple photos in another table, how can I select this sort of query so that I can grab the photos as well?

Upvotes: 0

Views: 81

Answers (1)

Mortalus
Mortalus

Reputation: 10712

Use joins: http://www.w3schools.com/sql/sql_join.asp

SELECT 
[NAME], 
[AGE], 
[GENDER],
[Photo]
From [People]
Inner join [PHOTOS] on People.ID = Photo.OwnerID

WHERE AGE = '20'

I assume that photo and people does not have same column names to avoid ambiguous column names. and i assume that there is a foreign key in the photo table that point sot a person.

Upvotes: 3

Related Questions