Arvix
Arvix

Reputation: 33

MySQL GROUP_CONCAT

I heve two Mysql tables

participants

id   |   name   | lastname |
----------------------------
1    |    Jon   |   Bush   |
2    |  Stephen |   Eagle  |

and

posts

id    |  parentid |  Title  |   text
--------------------------------------
1     |    1      |  Title1 |  Text1
2     |   1       | title3  |  text2
3     |      1    | title4  | text4
4     |      2    |   title |   ttext

And I need get out table

id (1) | Jon   | <a href='index.php?id='1'>Title1</a>, <a href='index.php?id='2'>title3</a>, <a href='index.php?id='3'>title4</a>
id (2) | Stephen | <a href='index.php?id='4'>title<a/>

So i need get out Title with hyperlink with contain ID.

I use

SELECT  a.ID, a.Name,
        GROUP_CONCAT(b.Title) TitleList
FROM    participants a
        INNER JOIN posts b
            ON a.ID = b.parentID
GROUP   BY  a.ID, a.Name

but in this case i can get out only Title row without ID row...

Regards

Upvotes: 1

Views: 6842

Answers (3)

Joseph Caracuel
Joseph Caracuel

Reputation: 974

select 
  a.id, 
  a.name,  
  group_concat(b.id) ids, 
  group_concat(b.title) titles,
  group_concat(concat("<a href='index.php?id=", b.id, "'>", b.title, "</a>")) titlelist
from participants a
inner join
  posts b on b.parentid = a.id
group by a.id,a.name

sql fiddle

I would suggest you assign the link into your PHP code.

Upvotes: 0

ashofphoenix
ashofphoenix

Reputation: 155

I guess this will do

SELECT a.ID, a.Name, GROUP_CONCAT(concat(b.Title,'-',b.id)) TitleList FROM participants a INNER JOIN posts b ON a.ID = b.parentID GROUP BY a.ID, a.Name

Upvotes: -1

hjpotter92
hjpotter92

Reputation: 80653

GROUP_CONCAT(b.Title) TitleList

should instead be:

GROUP_CONCAT( CONCAT("<a href='index.php?id=", b.id, "'", b.Title, "</a>") ) TitleList

Notice that it'll give you

<a href='index.php?id=1'>Title1</a>

which is correct way of hyper-linking.

Upvotes: 2

Related Questions