mysql - 仅在一列的 Left Join 上选择 distinct

标签 mysql sql

我正在尝试列出每个客户购买的产品,但如果他们在不同的场合购买了相同的产品,我希望将其排除在外。这是我目前所拥有的:

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

这是生产:

screenshot

基本上,无论他们购买了多少次,只要他们购买了产品,我就只需要列出一次。我该怎么做?

最佳答案

您可以在 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/

相关文章:

mysql - 在这种情况下如何使用外键?

mysql - 使用 'MA' 和 'Ma' 显示数据时出现困惑

MySQL Order by 2nd Column 不引用列名

mysql - 在mysql查询中忽略 "//"

python - 根据sql、r或python中的不同参数计算列之间的结果

mysql - 如何在mysql数据库上使用sum函数连续设置一个值?

当插入到 mysql 时,PHP 缺少动态表中的第一行复选框值?

mysql - 我的触发器语法不起作用

mysql如果没有记录

php - 我缺少什么? SQL注入(inject)