dourun2990 2013-05-03 02:34 采纳率: 100%
浏览 50

添加具有相同product_id的新字段后,数据会重复

I have two tables one called products the other product_images. product_images has 6 fields in this layout.

product_id, small_image, medium_image, large_image, width, height

small_image, medium_image and large_image are null fields.

My data has now duplicated after adding extra fields like small_image and large_image that have the same product_id.

My sql statement

"SELECT *,          
i.medium_image, i.width, i.height, 
    COALESCE((SELECT COUNT(*) 
    FROM order_details od 
    WHERE od.product_id = p.product_id), 0) as most_popular

FROM products p 
INNER JOIN product_images i on i.product_id = p.product_id
WHERE p.department_id=:department_id AND p.is_active=1
$orderby        
LIMIT :limit OFFSET :offset");

What do I need to do change my sql statement to only select the medium_image for the product so it won't duplicate, I tried using DISTINCT but that's not working. I just want to return the single product_image row that is medium_image related to the product_id

  • 写回答

1条回答 默认 最新

  • dongxiaoying5882 2013-05-03 02:48
    关注

    Try adding

    GROUP BY product_id
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题