簡體   English   中英

Mysql GROUP_CONCAT 根據計數相乘

[英]Mysql GROUP_CONCAT multiplies based on count

我正在嘗試進行一個 mysql 查詢,即 GROUP_CONCAT 是表 'shop_previews' 中的所有字符串。 但是,當運行以下查詢時,它會根據“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

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