SQL Server +选择行及其子表内容

时间:2013-04-25 02:20:21

标签: sql sql-server

我有这样简单的选择语句:

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'

所选人员可能在另一张桌子上有多张照片,如何选择此类查询以便我也可以抓取这些照片?

1 个答案:

答案 0 :(得分:3)

使用联接: 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'

我认为照片和人没有相同的列名,以避免模糊的列名。 并且我假设照片表中有一个指向一个人的外键。