SQL Server 2008查询从两个表中获取混合数据

时间:2012-09-04 18:26:08

标签: sql-server-2008

有人请告诉我SQL查询以获得结果....

谢谢!

enter image description here

从上面的两个表格中,我想从[{1}}获取Photo的{​​{1}},NameId of max(id) Table-A category_id } parent_id中的Table-B

1

2 个答案:

答案 0 :(得分:2)

select a.id,a.name,a.photo,b.category_name,b.category_id
from table-A a join table-B s ON a.category_id = b.category_id
where parent_id = 1

答案 1 :(得分:1)

尝试以下内容;

Select TBLA.ID, TBLA.Name, TBLA.Photo, TBLB.Category_Name, TBLB.Category_ID
From [table-B] TBLB
Inner Join [table-a] TBLA On TBLA.Category_ID = TBLB.Category_ID
Where TBLB.Parent_ID = 1 
And TBLA.ID = (Select Max(ID) 
      From [table-a]
      Where Category_ID = TBLB.Category_ID)