日期:2014-05-18  浏览次数:20818 次

SQL SELECT 多表查询
商品表(product):
如图:

在线咨询表(consultation):
如图:

商品图片表(productphotos):
如图:


问题是如何查询出这样的结果集?
比如: 




------解决方案--------------------
SQL code
select top(1) b.id,b.title,b.content,b.productId,a.producttitle,a.promotionPrice,c.ProductPhoto
from product a ,consultation b ,productphotos c
where a.productId = b.productId and a.productId = c.productId
group by b.id,b.title,b.content,b.productId,a.producttitle,a.promotionPrice
order by c.ReleaseTime desc

------解决方案--------------------
try
SQL code
SELECT commt.Id,commt.Title,commt.Content,commt.ProductID,prod.ProductTitle,prod.PromotionPrice,prodimg.ProductPhoto 
FROM commentary AS commt 
INNER JOIN product AS prod on(commt.ProductID=prod.Id)
INNER JOIN productphotos AS prodimg ON(prod.Id=prodimg.ProductID)
WHERE prodimg.releasetime=(select top 1 releasetime from productphotos where ProductID=commt.ProductID and releasetime>=commt.dateandtime order by releasetime)