sql语句去重查询(跪请大神帮忙。。。)

CrystalAngelLee 2018-02-10 04:01:32
sql语句:
“select p.pid,p.title,p.subtitle,p.price,p.href,m.standard_pic from yc_product p,yc_product_pic m where p.pid=m.pid and p.tid=1”

每一个商品(对应每一个pid)对应有不同的图片,但是不在同一张表中,想要从两张表中都查询出东西并且每个商品只想保留一张图片(只显示一个 standard_pid),用过distinct 和 group by 都不行
大神们帮帮忙
...全文
2402 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
道素 2018-02-14
  • 打赏
  • 举报
回复
你可以这样试试,和楼上的思路差不多

      select p.pid,p.title,p.subtitle,p.price,p.href,m.standard_pic 
      FROM yc_product p
      LEFT JOIN (
           SELECT ROW_NUMBER()OVER(PARTITION BY m.pid ORDER BY standard_pic) AS seq,* FROM yc_product_pic m
      ) AS mm ON mm.seq=1 AND mm.pid=p.pid
吉普赛的歌 2018-02-11
  • 打赏
  • 举报
回复
SELECT p.pid,
       p.title,
       p.subtitle,
       p.price,
       p.href,
       (SELECT TOP 1 m.standard_pic FROM yc_product_pic AS m WHERE p.pid=m.pid) AS standard_pic
FROM   yc_product AS p
WHERE  p.tid = 1
Secends_ 2018-02-11
  • 打赏
  • 举报
回复
with m as( select pid,title,subtitle,price,href,standard_pic row_number()over(partition by pid order by standard_pic )rn from (select p.pid,p.title,p.subtitle,p.price,p.href,m.standard_pic from yc_product p,yc_product_pic m where p.pid=m.pid and p.tid=1 ) ) select * from m where rn=1 你试试 看看行不行 是不是 你想要的
Secends_ 2018-02-11
  • 打赏
  • 举报
回复
引用 3 楼 Secends_ 的回复:
with mm as( select pid,title,subtitle,price,href,standard_pic row_number()over(partition by pid order by standard_pic )rn from (select p.pid,p.title,p.subtitle,p.price,p.href,m.standard_pic from yc_product p,yc_product_pic m where p.pid=m.pid and p.tid=1 ) ) select * from mm where rn=1 你试试 看看行不行 是不是 你想要的
刚才没注意你用了m了 with mm换掉
  • 打赏
  • 举报
回复
groupby 除开picture 那个字段,select picture聚合下

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧