Da_CommodityList是商品表,Da_CommodityComment 评论表 pid为商品id
SELECT row_number() OVER ( ORDER BY time DESC ) AS r_n ,
comm.time ,
comm.pid ,
comm.users ,
comm.contents ,
comlist.pic_url ,
comlist.p_name ,
comlist.cur_price ,
comlist.p_o_url ,
comlist.web_name
FROM Da_CommodityList comlist
LEFT JOIN Da_CommodityComment comm ON comlist.pid = comm.pid
WHERE comlist.pid IN ( 22390055, 22240438, 22240355, 20761416, 22390312,24341202 )
如下图虽然每一列都不同,但是又重复的pid,怎么去除重复的pid的一列,只保留一个
答案为:
SELECT row_number() OVER ( ORDER BY time DESC ) AS r_n ,* FROM (SELECT
comm.time ,
comm.pid ,
comm.users ,
comm.contents ,
comlist.pic_url ,
comlist.p_name ,
comlist.cur_price ,
comlist.p_o_url ,
comlist.web_name,
row_number() OVER ( PARTITION BY comm.pid ORDER BY comm.time DESC ) AS che --按 pid 分组 按 time 排序
FROM Da_CommodityList comlist
LEFT JOIN Da_CommodityComment comm ON comlist.pid = comm.pid)
a
WHERE pid IN ( 22390055, 22240438, 22240355, 20761416, 22390312,24341202 ) AND che=1
结果为下图