我正在尝试列出每个客户购买的产品,但如果他们在不同的场合购买了相同的产品,我希望将其排除在外。这是我目前所拥有的:
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_concat意识到这个事实,它有 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
From the docs The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet. The syntax to change the value of group_concat_max_len at runtime is as follows, where val is an unsigned integer:
SET [GLOBAL | SESSION] group_concat_max_len = val;
关于mysql - 仅在一列的 Left Join 上选择 distinct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22207819/