商品表(product):
如图:在线咨询表(consultation):
如图:商品图片表(productphotos):
如图:
问题是如何查询出这样的结果集?
比如: 

解决方案 »

  1.   

    mayuanf,您好。
    我现在商品图片表中每一个产品可能有三张以上的照片,不是一张,利用join查询,如何筛选出最新上传的产品的那个图片?
      

  2.   

    select 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
      

  3.   

    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
    order by c.ReleaseTime desc
      

  4.   


     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)结果如图所示:(显示交叉结果集,并不是我想要的。)
      

  5.   

    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
      

  6.   

    梦想鳖,您好。
    若把top(1)去掉的话就会出现重复,而把top(1)加上去的话,查询出来的记录就有一条。但是我数据库里面是有两条记录的。
      

  7.   

    加上top(1)显示的结果集,如下图所示:去掉top(1)显示的结果集,如下图所示:
      

  8.   

    这是我调试的代码:select top(1) b.id,b.title,b.content,b.ProductID,a.producttitle,a.promotionPrice,c.ProductPhoto
    from product a ,commentary b ,productphotos c
    where a.Id = b.ProductID and a.Id = c.ProductID
    group by b.id,b.title,b.content,b.productId,a.producttitle,a.promotionPrice,c.ProductPhoto,c.ReleaseTime
    order by c.ReleaseTime desc
    select b.id,b.title,b.content,b.ProductID,a.producttitle,a.promotionPrice,c.ProductPhoto
    from product a ,commentary b ,productphotos c
    where a.Id = b.ProductID and a.Id = c.ProductID
    group by b.id,b.title,b.content,b.productId,a.producttitle,a.promotionPrice,c.ProductPhoto,c.ReleaseTime
    order by c.ReleaseTime desc
      

  9.   

    这是我刚刚调试的查询语句:select top(1) b.id,b.title,b.content,b.ProductID,a.producttitle,a.promotionPrice,c.ProductPhoto
    from product a ,commentary b ,productphotos c
    where a.Id = b.ProductID and a.Id = c.ProductID
    group by b.id,b.title,b.content,b.productId,a.producttitle,a.promotionPrice,c.ProductPhoto,c.ReleaseTime
    order by c.ReleaseTime desc
    select  b.id,b.title,b.content,b.ProductID,a.producttitle,a.promotionPrice,c.ProductPhoto
    from product a ,commentary b ,productphotos c
    where a.Id = b.ProductID and a.Id = c.ProductID
    group by b.id,b.title,b.content,b.productId,a.producttitle,a.promotionPrice,c.ProductPhoto,c.ReleaseTime
    order by c.ReleaseTime desc
      

  10.   

    咨询表、商品表、商品图片表的关联性是商品编号,即字段ProductID。
      

  11.   

    咨询表、商品表、商品图片表的关联性为商品编号,即字段ProductID。
      

  12.   


    这个就是问题的所在,一个ProductID可能对应有多个图片,一个咨询记录对应一个ProductID,就意味这一个咨询记录可能对应多个图片,
    但你现在想一个咨询记录取一张图片,如何取?依据是什么?
      

  13.   

    josy
    (百年树人),您好。
    我的思路是这样的,首先用内边接查询出咨询表与商品表之间的共同的数据,然后查询出某个商品最新上传的图片信息,最后,能否合并到一起成为结果集。按道理就应该可以实现了。
      

  14.   

    try
    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)
      

  15.   

    josy
    (百年树人),您好。
    刚刚调试了一下,无结果集。而去掉and releasetime>=commt.dateandtime之后,显示正常。有点不理解。在此,我非常感谢您这么晚仍在帮我解决问题。谢谢。
      

  16.   

    josy
    (百年树人),您说的是啊。
    很荣幸能够通过这个问题结识到您,希望能够与您成为好朋友。
    我的联系方式:15026976866 QQ:215645471。如有需要帮助,请联系我。