There are 2 tables in mysql. One stores information about the group, the other stores lists by group id.

SELECT * FROM `group`,`dialog` WHERE `group`.id = `dialog`.group_id LIMIT 50 

This example displays as many groups as there are lists. It is necessary to display one group and the entire list for this group.

Tell me please.

2 answers 2

If I correctly understood what you want (I advise you to specify the structure of the tables in the question), use the GROUP_CONCAT function:

 SELECT g.id, GROUP_CONCAT(d.spisok) FROM `group` g join `dialog` d ON g.id = d.group_id GROUP BY g.id LIMIT 50 

An example on sqlfiddle .

  • one
    If you guessed correctly, then the question would be to close on the double because there are already 100500 such questions / answers - Mike
  • And how to output it through a cycle? For I am not in trouble with groups of records, I don’t understand how to display the group id first and after the list ... Or do I need 2 cycles? - Albert Ushakov
  • one
    @Mike in general you are right, they are already here in bulk of such questions. - Denis
  • @Albert Ushakov cycle is not needed, GROUP_CONCAT will do everything. look at the example on sqlfiddle, updated the answer. - Denis
  • It does not work for me. I deduce d.spisok in the form of $ group ['spisok'] and nothing (In the place of spisok, respectively, its field). But the groups are displayed correctly. Lists are not. - Albert Ushakov
  $sql_dialog = mysql_query("SELECT f.*, GROUP_CONCAT(d.d_title) AS dialog_title FROM fandom f join dialog d ON f.id = d.d_fandom GROUP BY f.id"); while($dialog = mysql_fetch_array($sql_dialog)){ $list_vivod = null; $dialog_list = explode(',',$dialog['dialog_title']); foreach($dialog_list AS $list){ $list_vivod .= '<list class="list-box-spisok">'.$list.'</list>'; } print<<<HERE $dialog[id] $list_vivod HERE; }