如题,一个product表,一个comment表,关联是 product.id = comment.productId
要求查出 最新评论的50个产品 
貌似是个比较简单的sql,不知咋回事就是写不出来了

解决方案 »

  1.   


    select top 50 * from product inner join comment on product.id = comment.productId 再加上评价的时间 desc
      

  2.   

    product表,一个comment表
    select * from (SELECT *,ROW_NUMBER() OVER(ORDER BY 时间 desc ,id desc) as num FROM PRODUCT JOIN comment on product.id = comment.productId) a where a.num>=50一定要加id
      

  3.   

    用评论的时间排序,用(top 50)取出前50条不就行了
      

  4.   


    SELECT * FROM TABLE A 
    CROSS APPLY
    (
    SELECT TOP(50) * FROM TABLE B WHERE A.ID=B.ID
    ) B
      

  5.   


    select * from product where id in
    (select top 50 productId from comment t where not exists(select 1 from comment where productId=t.productId
    and id>t.id))
      

  6.   

    select P.*,C.* from product P join comment C on C.productId =P.id order by '评论时间'
      

  7.   

    select top 50 P.*,C.* from product P join comment C on C.productId =P.id order by '评论时间'
      

  8.   

    --如果有评论时间则,按照评论时间排序
    select top 50 P.*,C.* from product P join comment C on C.productId =P.id order by '评论时间'
    --如果有自增ID ,则按照ID排序
    select top 50 P.*,C.* from product P join comment C on C.productId =P.id order by 自增ID
    --楼主不妨给点数据,别让大家猜
      

  9.   

    哦,是我没说清楚,comment是有一个insertTime的
    使用
    select top 50 P.*,C.* from product P join comment C on C.productId =P.id order by '评论时间'
    这样的语句有这样的一个问题——
    如果用户对一个产品连续评论了50次
    那么通过这条语句查出来的产品应该是50个相同的产品
    而我希望查出来的是前50个不同的产品
      

  10.   

    distinct、group by 可以过滤重复
      

  11.   

    如题,一个product表,一个comment表,关联是 product.id = comment.productId SELECT TOP(50) FROM PRODUCT A LEFT OUTER JOIN COMMENT ON product.id = comment.productId 
      

  12.   

    select top 50 * from tb order by 时间 desc