P
P
pibiv2014-12-08 17:45:12
MySQL
pibiv, 2014-12-08 17:45:12

How to properly concatenate strings in SQL?

There is a query in the database of the form

SELECT 
users.name AS users_name, 
users.uid AS uid, 
users.created AS users_created,
domain_editor.domain_id AS domain_editor_domain_id, 
'user' AS field_data_field_user_statut_user_entity_type

FROM 
users

LEFT JOIN  domain_editor ON users.uid = domain_editor.uid
WHERE (( (users.status <> '0') ))
ORDER BY users_created DESC
LIMIT 10 OFFSET 0

Which at the output gives such a result
f32a2dd7827c47bcad38d6f6cd20d0c5.png
. That is, the same user with different domain id is shown 3 times.
The client wants the user to be shown only once and the domain id is separated by commas.
The question is how to implement it correctly?
Tried to use something like
GROUP_CONCAT(DISTINCT domain_editor.domain_id ORDER BY domain_editor.domain_id ASC SEPARATOR ',')

But alas, I can't get the right result in the end.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Entelis, 2014-12-08
@pibiv

In group by list all fields except domain_id.

select 
users.name AS users_name, 
users.uid AS uid, 
users.created AS users_created,
GROUP_CONCAT(DISTINCT domain_editor.domain_id ORDER BY domain_editor.domain_id ASC SEPARATOR ',') AS domain_editor_domain_id_concated, 
'user' AS field_data_field_user_statut_user_entity_type
...
group by users_name, uid, users_created, field_data_field_user_statut_user_entity_type

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question