Is it possible to concat (in one field), all rows from result.
My table is like that :
ID STRING
1 a,aa
2 xxx
3 str
4 aa,xx
And I want somthing like:
ID STRING CONCAT
1 a,aa a,aa xxx str aa,xx
2 xxx a,aa xxx str aa,xx
3 str a,aa xxx str aa,xx
4 aa,xx a,aa xxx str aa,xx
I tried with GROUP_CONCAT, but that doesn't do what I want ..
Any idea ? Bye
UPDATE 2:
Actually, here is my request
SELECT DISTINCT
(SELECT GROUP_CONCAT(LABEL_ID) FROM DOCUMENT WHERE ENTITY_ID = `p`.id) as label,
`p`.*,
(6371*acos(cos(radians(48.8817))*cos(radians(LATITUDE))*cos(radians(LONGITUDE) - radians(2.3822))+sin(radians(48.8817))*sin(radians(LATITUDE)))) AS `dist`,
`a`.`NUMBER_COMM`,
`a`.`AVE`
FROM `PRO` AS `p`
INNER JOIN `PRO_JOB_RELATION` AS `j_rel` ON p.ID = j.ID_PRO
INNER JOIN `COMM` AS `a` ON p.ID = a.ID_PRO WHERE (p.ETAT = 1)
AND (j.ID_METIER = '15')
HAVING (dist <= 300 and dist <= `p`.RAYON)
ORDER BY ((AVE * 4) + ((5 * NUMBER_COMM ) / 100)) - ((( 5 * dist) / 100) * 20) / 3 DESC, `CHECKED` DESC
And I got this result :
ID LABEL OTHER
1 2,3 ...
2 NULL ...
3 2 ...
4 8,2 ...
And what I really need is something like that :
ID LABEL OTHER LABEL_CONCAT
1 2,3 ... 2,3,8
2 NULL ... 2,3,8
3 2 ... 2,3,8
4 8,2 ... 2,3,8
And I can't do that ... Can you help me ?
You can use group_concat, but in a subquery
select id, string, (select group_concat(string SEPARATOR ' ')
from Table1)
from Table1
see SqlFiddle
Yes. It is possible. And you can do it using the GROUP_CONCAT
aggregate function.
But the result you specified would require either a subquery in the SELECT list, or a JOIN to an inline view, eg
SELECT t.id AS `ID
, t.string AS `STRING`
, c.gc AS `CONCAT`
FROM mytable t
CROSS
JOIN ( SELECT GROUP_CONCAT(s.string ORDER BY s.id) AS `gc`
FROM mytable s
) c
ORDER BY t.id
Note that the return from GROUP_CONCAT
is limited, by some system variables, max_group_concat_len
(?something like that) and max_allowed_packet
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.