mysql query join group_concat

user3117183

I have the following query,

 SELECT 
     p.id AS parent, GROUP_CONCAT( c.id ) AS children
 FROM 
     rev_r_clients AS p
 JOIN 
     rev_r_clients AS c ON c.parent_client_id = p.id
 GROUP BY 
     p.id

It get the id as parent and the children of the parent. After that I send the mapping id to array using the following function,

$parents = Array();

while ($row = $res->fetch_assoc()) {
    $parents[$row['parent']] = explode(',',$row['children']);
}

I need it to get a username from another table (rev_users) as well, how could this be done?

user2923779

If you don't want to achieve this with two queries (or alternatively one MySQL transaction using a subquery) then you could concat like this:

SELECT 
    p.id AS parent_id, p_u.name, GROUP_CONCAT( CONCAT(c.id, '-', c_u.name) ) AS children
FROM 
    rev_r_clients AS p
JOIN 
    rev_r_clients AS c ON c.parent_client_id = p.id
JOIN 
    rev_users c_u ON c_u.id= c.id
JOIN 
    rev_users p_u ON p.id = p_u.id
GROUP BY 
    p.id

Be careful doing this though. There are limits on how big a GROUP_CONCAT can get. See the documentation http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

And your code would look like

$parents = Array();
while($row = $res->fetch_assoc()) {
  $children = explode(',',$row['children']);
  foreach($children as $child){
    $child_info = explode('-', $child);
    if(!isset($parents[$row['parent_id']])){
      $parents[$row['parent']] = array(
        'id' => $row['parent'],
        'name' => $row['name'],
        'children' => array()
      )
    }
    $parents[$row['parent']]['children'][] = array(
      'id' => $child_info[0],
      'name' => $child_info[1]
    );
  }
}

Lastly be careful to use a separator in your CONCAT function that you know will not be in the name.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mysql JOIN with GROUP_CONCAT in a complex query

From Dev

Using Mysql GROUP_CONCAT in JOIN Query

From Dev

MySQL group_concat with join

From Dev

Mysql GROUP_CONCAT and IN query

From Dev

mySQL GROUP_CONCAT - Query

From Dev

Mysql with Group by + IF + Left Join + Group_Concat

From Dev

mysql inner join group_concat mysql

From Dev

join query with group_concat, duplicate values

From Dev

mysql request using join and group_concat

From Dev

mysql request using join and group_concat

From Dev

Optimize MySQL query for group_concat function

From Dev

Using MYSQL GROUP_CONCAT with sub query

From Dev

MySQL GROUP_CONCAT Query Excluding Records

From Dev

Mysql query using IN with group_concat result

From Dev

Using MYSQL GROUP_CONCAT with sub query

From Dev

Mysql group_concat query gives error

From Dev

Select query with GROUP_CONCAT in mysql

From Dev

Join and group concat mysql query not working as expected

From Dev

how to use select query in a Group_concat sub query in mysql

From Dev

Converting MySQL code to Access: GROUP_CONCAT and a triple JOIN

From Dev

Double results in GROUP_CONCAT with double JOIN in MySQL

From Dev

MySql duplicated values in a join using GROUP_CONCAT

From Dev

mysql self join with group_concat and without duplicates

From Dev

Search running spatially with PHP, MYSQL, GROUP_CONCAT and JOIN

From Dev

Mysql 5.6.12 Group_concat with Inner Join Issue

From Dev

MySql duplicated values in a join using GROUP_CONCAT

From Dev

Error when I used GROUP_CONCAT on mysql query

From Dev

Issue with GROUP_CONCAT AND ORDER BY FIELD in MySQL query

From Dev

MySQL Join Query GROUP BY

Related Related

HotTag

Archive