【问题标题】:Mysql GROUP_CONCAT multiplies based on countMysql GROUP_CONCAT 基于计数相乘
【发布时间】:2020-05-23 00:07:27
【问题描述】:

我正在尝试对 GROUP_CONCAT 表“shop_previews”中的所有字符串进行 mysql 查询。 但是,在运行以下查询时,它会根据“timesBought”的数量重复它。

select 
    `shop_items`.*, 
     group_concat(shop_previews.image) as images, 
     count(`shop_histories`.`shopId`) as `timesBought` 
from `shop_items`
left join `shop_previews` on `shop_previews`.`shopId` = `shop_items`.`id`
left join `shop_histories` on `shop_items`.`id` = `shop_histories`.`shopId`
group by `shop_items`.`id` 
order by `timesBought` desc

因此,如果 timesBought 等于 2,并且 shop_previews.image 中只有一个结果,它将将该条目与自身连接起来,从而产生类似

https://example.com/example.png,https://example.com/example.png

【问题讨论】:

    标签: mysql sql group-by group-concat


    【解决方案1】:

    GROUP_CONCAT()支持DISTINCT

    GROUP_CONCAT(DISTINCT shop_previews.image) as images
    

    【讨论】:

      猜你喜欢
      • 2017-10-23
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 2012-04-13
      • 2018-05-11
      • 2018-11-19
      • 1970-01-01
      相关资源
      最近更新 更多