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 | Title1, title3, title4
------------------------------
id (2) | Stephen | title
I try do this with
$result = mysql_query("SELECT name, Title, participants.id, parent FROM aposts, participants WHERE paricipants.id = parent.parent group by last_name ORDER BY .......");
But in this cas I cant get loop on parent to get out all posts of this parent... Maybe someone can help me....
I'm not sure if this is exactly what you want. Seeing your example, you want to return Title
separated by comma for every ID
and Name
. MySQL has a builtin function called GROUP_CONCAT
which concatenate rows instead of columns.
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
OUTPUT
╔════╦═════════╦══════════════════════╗
║ ID ║ NAME ║ TITLELIST ║
╠════╬═════════╬══════════════════════╣
║ 1 ║ Jon ║ Title1,title3,title4 ║
║ 2 ║ Stephen ║ title ║
╚════╩═════════╩══════════════════════╝