简体   繁体   English

Mysql GROUP_CONCAT 根据计数相乘

[英]Mysql GROUP_CONCAT multiplies based on count

I'm trying to make a mysql query that GROUP_CONCAT's all strings from the table 'shop_previews'.我正在尝试进行一个 mysql 查询,即 GROUP_CONCAT 是表 'shop_previews' 中的所有字符串。 When running the following query however, it repeats it based on the amount of "timesBought".但是,当运行以下查询时,它会根据“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

So if timesBought equals to two, and there was only one result from shop_previews.image, it would concat that one entry with itself, resulting in something like因此,如果 timesBought 等于 2,并且 shop_previews.image 中只有一个结果,则它会将该条目与自身连接起来,从而产生类似

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

GROUP_CONCAT()支持DISTINCT

GROUP_CONCAT(DISTINCT shop_previews.image) as images

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM