2013-04-06 261 views
1

我HEVE两个MySQL表MySQL的GROUP_CONCAT

参与者

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

帖子

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

我需要出去表

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/> 

所以我需要拿出包含ID的超链接标题。

我用

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 

但在这种情况下,我可以出去只有标题行没有ID列...

问候

+2

所以你试图直接从查询中检索超链接?它可以完成,但最好将它放到PHP代码中的表示逻辑中。 – 2013-04-06 11:52:55

+0

不,它可以是PHP的脚本。但我如何可以将Id行作为变量? – Arvix 2013-04-06 11:55:14

+0

我完全放弃了'GROUP_CONCAT()',只需选择你需要的列。获取后,构建输出循环中的链接。您只需要一个变量来测试循环中“a.ID”更改的时间。 – 2013-04-06 12:35:50

回答

2
GROUP_CONCAT(b.Title) TitleList 

应改为:

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

注意,它会给你

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

这是超链接的正确方法。

-1

我想这会做

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

0
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

我建议你指定的链接到你的PHP代码。