我试着列出每个顾客购买的产品,但是如果他们在不同的场合购买了相同的产品,我希望它将其排除在外。这就是我目前所拥有的:
Select c.field_id_33 AS email, o.order_id, Group_concat(o.entry_id) AS Products,group_concat(t.title),group_concat(t.url_title) from finn_cartthrob_order_items o
LEFT JOIN finn_channel_data c
ON c.entry_id=o.order_id
LEFT JOIN finn_channel_titles t
ON o.entry_id=t.entry_id
GROUP BY email
这产生了:
基本上,我只需要列出一次产品,如果他们购买了,无论他们购买了多少次。我该怎么做?
最佳答案
您可以在group concat函数中使用DISTINCT
,使用Group_concatbaware这一事实,它的默认限制是1024个字符,但可以增加
Select c.field_id_33 AS email, o.order_id,
Group_concat(DISTINCT o.entry_id) AS Products,
group_concat(DISTINCT t.title),
group_concat(DISTINCT t.url_title)
from finn_cartthrob_order_items o
LEFT JOIN finn_channel_data c
ON c.entry_id=o.order_id
LEFT JOIN finn_channel_titles t
ON o.entry_id=t.entry_id
GROUP BY email
从DOCS中,结果被截断为最大长度,即
由group U concat U max U len系统变量给定,该变量具有默认值
值1024。该值可以设置得更高,尽管
返回值的最大长度受值的限制。
允许的最大数据包。更改值的语法
运行时的组concat_max_len如下,其中val是
无符号整数:
设置[GLOBAL | SESSION]group_concat_max_len=val;
关于mysql - 在左联接中选择唯一的一列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22207819/