3
**********************
* id  *  typ  *  cl  *
**********************
* 1   *  1    *  1   *
* 2   *  4    *  1   *
* 3   *  7    *  1   *
* 4   *  2    *  2   *
* 5   *  4    *  2   *
* 6   *  8    *  2   *
**********************

hi frnz, I have a problem with mysql group by... tired of looking on the google so finally posting it here... my table structure is as showing above. I am using the following query...

$sql2w=executeQuery("select * from logo group by cl  ");
    while($line2=mysql_fetch_array($sql2)){ 
    echo $line2['typ'];
}

I am getting 1 and 2 in the result, but I want the result in this format.. 1,4,7 and 2,4,8 in string or in array format...

I hope I am clear what I want...

I am only getting the first row values of "typ" column but I want all rows values in array after grouping the "cl" column..

Thanks in advance

1
  • @eggyal: I'm guessing "friends"? Commented Sep 28, 2013 at 18:44

1 Answer 1

7

Use GROUP_CONCAT():

SELECT cl, GROUP_CONCAT(typ) FROM logo GROUP BY cl

See it on sqlfiddle.

Sign up to request clarification or add additional context in comments.

2 Comments

@user2437172: Unless you alias it otherwise, the resultset column will be named GROUP_CONCAT(typ), so you would need to access it accordingly - echo $line2['GROUP_CONCAT(typ)'];.
as a variant you can use some alias for group concat for example GROUP_CONCAT(typ) as typs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.