简体   繁体   English

无法在联接上用GROUP_CONCAT合并值

[英]Can't concat values with GROUP_CONCAT on joins

Could anybody help me? 有人可以帮我吗?

With this query I am getting the ids, but it is not putting the separators when subscriber_data.fieldid is null. 通过此查询,我获得了ID,但是当Subscriber_data.fieldid为null时,它没有放置分隔符。 For example instead of coming 2,,12 it comes 2,12 when the value for 4 is null... 例如,当4的值为null时,它不是2,2,12,而是2,12。

I think the problem is on the Join with subquery, but i couldn't make it with two left joins in the main query also... 我认为问题出在与子查询的联接上,但我也无法在主查询中通过两个左联接实现它...

This is the query im using: 这是即时通讯使用的查询:

SELECT 
    list_subscribers.emailaddress, 
    (SELECT 
         GROUP_CONCAT(IFNULL(customfields.fieldid,'') SEPARATOR '","') 
     FROM customfields 
     LEFT JOIN subscribers_data 
         ON subscribers_data.fieldid = customfields.fieldid 
     WHERE 
         customfields.fieldid IN (2,4,12,13,14,15,17,19,20,21,22,23,16,26,27) 
             AND 
         list_subscribers.subscriberid = subscribers_data.subscriberid
    ) AS data FROM list_subscribers

Thanks everyone. 感谢大家。

The left join is useless. 左联接是无用的。 Because you have a condition on subscriber_data in the WHERE clause, that subquery will not return those rows for which there is no matching subscriber_data, so it effectively works as if you used INNER JOIN. 因为您在WHERE子句中对subscriber_data有一个条件,所以该子查询将不会返回没有匹配的subscriber_data的那些行,因此它就像使用INNER JOIN一样有效。 You should add that condition to the left join condition, but it is impossible in this query layout. 您应该将该条件添加到左联接条件,但是在此查询布局中是不可能的。 Values from the outer query are not allowed in join conditions in the inner query. 内部查询的联接条件中不允许外部查询的值。

You could change it, but apparently you need to join three tables, where the middle table, subscriber_data, that links them all together, is optional. 您可以对其进行更改,但是显然您需要连接三个表,其中将它们链接在一起的中间表“ users_data”是可选的。 That doesn't really make sense. 那真的没有道理。

Or maybe customfields is the table that is optional, but in that case, you should have reversed the table or used a RIGHT JOIN. 也许customfields是可选的表,但是在那种情况下,您应该颠倒该表或使用RIGHT JOIN。

In conclusion, I think you meant to write this: 总而言之,我认为您的意思是:

select
  s.emailaddress,
  GROUPCONCAT(IFNULL(f.fieldid, '') SEPARATOR '","')
from
  list_subscribers s
  inner join subscribers_data d on d.subscriberid = s.subscriberid
  left join customfields f on f.fieldid = d.fieldid
where
  d.fieldid in (2,4,12,13,14,15,17,19,20,21,22,23,16,26,27)
group by
  s.emailaddress

Or do you want to list the id's of the fields that are filled for the subscriber(s)? 还是要列出为订阅者填写的字段的ID? In that case, it would be: 在这种情况下,它将是:

select
  s.emailaddress,
  GROUPCONCAT(IFNULL(d.fieldid, '') SEPARATOR '","')
from
  list_subscribers s
  cross join customfields f
  left join subscribers_data d on 
    d.subscriberid = s.subscriberid and
    d.fieldid = f.fieldid
where
  f.fieldid in (2,4,12,13,14,15,17,19,20,21,22,23,16,26,27)
group by
  s.emailaddress

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

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