首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL SELECT 多表查询解决思路

2012-03-27 
SQL SELECT 多表查询商品表(product):如图:在线咨询表(consultation):如图:商品图片表(productphotos):如

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

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

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


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




[解决办法]

SQL code
select top(1) b.id,b.title,b.content,b.productId,a.producttitle,a.promotionPrice,c.ProductPhotofrom product a ,consultation b ,productphotos cwhere a.productId = b.productId and a.productId = c.productIdgroup by b.id,b.title,b.content,b.productId,a.producttitle,a.promotionPriceorder 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) 

热点排行