select top 50 * from product inner join comment on product.id = comment.productId 再加上评价的时间 desc
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
用评论的时间排序,用(top 50)取出前50条不就行了
SELECT * FROM TABLE A CROSS APPLY ( SELECT TOP(50) * FROM TABLE B WHERE A.ID=B.ID ) B
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))
select P.*,C.* from product P join comment C on C.productId =P.id order by '评论时间'
select top 50 P.*,C.* from product P join comment C on C.productId =P.id order by '评论时间'
--如果有评论时间则,按照评论时间排序 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 --楼主不妨给点数据,别让大家猜
哦,是我没说清楚,comment是有一个insertTime的 使用 select top 50 P.*,C.* from product P join comment C on C.productId =P.id order by '评论时间' 这样的语句有这样的一个问题—— 如果用户对一个产品连续评论了50次 那么通过这条语句查出来的产品应该是50个相同的产品 而我希望查出来的是前50个不同的产品
distinct、group by 可以过滤重复
如题,一个product表,一个comment表,关联是 product.id = comment.productId SELECT TOP(50) FROM PRODUCT A LEFT OUTER JOIN COMMENT ON product.id = comment.productId
select top 50 * from product inner join comment on product.id = comment.productId 再加上评价的时间 desc
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
SELECT * FROM TABLE A
CROSS APPLY
(
SELECT TOP(50) * FROM TABLE B WHERE A.ID=B.ID
) B
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))
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
--楼主不妨给点数据,别让大家猜
使用
select top 50 P.*,C.* from product P join comment C on C.productId =P.id order by '评论时间'
这样的语句有这样的一个问题——
如果用户对一个产品连续评论了50次
那么通过这条语句查出来的产品应该是50个相同的产品
而我希望查出来的是前50个不同的产品