加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SqlServer去除表中的重复列,商品表和评论表,一个相拼对应多个

发布时间:2020-12-12 13:39:29 所属栏目:MsSql教程 来源:网络整理
导读:Da_CommodityList是商品表,Da_CommodityComment 评论表 pid为商品id SELECT row_number() OVER ( ?ORDER BY time DESC ) AS r_n, ? ? ? ? comm.time,tahoma; font-size:14px; line-height:24px">? ? ? ? comm.pid,tahoma; font-size:14px; line-height:24px

Da_CommodityList是商品表,Da_CommodityComment 评论表 pid为商品id

SELECT row_number() OVER ( ?ORDER BY time DESC ) AS r_n,
? ? ? ? comm.time,tahoma; font-size:14px; line-height:24px">? ? ? ? comm.pid,tahoma; font-size:14px; line-height:24px">? ? ? ? comm.users,tahoma; font-size:14px; line-height:24px">? ? ? ? comm.contents,tahoma; font-size:14px; line-height:24px">? ? ? ? comlist.pic_url,tahoma; font-size:14px; line-height:24px">? ? ? ? comlist.p_name,tahoma; font-size:14px; line-height:24px">? ? ? ? comlist.cur_price,tahoma; font-size:14px; line-height:24px">? ? ? ? comlist.p_o_url,tahoma; font-size:14px; line-height:24px">? ? ? ? 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,24341202 )? AND che=1

结果为下图

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读