php - mysql parent id loop -
i heve 2 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 need out table
-------------------------- id (1) | jon | title1, title3, title4 ------------------------------ id (2) | stephen | title
i try with
$result = mysql_query("select name, title, participants.id, parent aposts, participants paricipants.id = parent.parent group last_name order .......");
but in cas cant loop on parent out posts of parent... maybe can me....
i'm not sure if want. seeing example, want return title
separated comma every id
, name
. mysql has builtin function called group_concat
concatenate rows instead of columns.
select a.id, a.name, group_concat(b.title) titlelist participants inner join posts b on a.id = b.parentid group a.id, a.name
output
╔════╦═════════╦══════════════════════╗ ║ id ║ name ║ titlelist ║ ╠════╬═════════╬══════════════════════╣ ║ 1 ║ jon ║ title1,title3,title4 ║ ║ 2 ║ stephen ║ title ║ ╚════╩═════════╩══════════════════════╝
Comments
Post a Comment