: SELECT   r.id, r.title,   u.name AS 'created_by',   GROUP_CONCAT( CONCAT(CAST(c.id as CHAR),',',c.name,',',c.value) separator ';') AS 'Categories' FROM   resource r   INNER JOIN  /*Select matching records as a table*/   (SELECT    DISTINCT r.id AS id   FROM    resource r   INNER JOIN    category_resource cr0 ON (r.id = cr0.resource_id)   INNER JOIN    category_resource cr0 ON (r.id = cr0.resource_id) 