Hello i have a problem with a Sql script

SELECT col1_id, col1_name, col2 FROM table GROUP BY ?????

col1_id col1_name col2
A john bear
A john dog
A john cat
A john bear
A john dog
A john cat
A john dog
A john cat
A john bear
A john dog
A john cat
A john bear
A john dog
A john cat
A john dog
A john cat

I want to return all results. It has to return like this: echo col1_id . col1_name (A john) and then col2 (bear,dog, cat)

Recommended Answers

You can use group_concat funtion

SELECT 
    col1_id,
    col1_name,

    GROUP_CONCAT(DISTINCT col2
        ORDER BY col2
        SEPARATOR ';')
FROM
    table

GROUP BY col1_id, col1_name

http://www.mysqltutorial.org/mysql-group_concat/

Jump to Post

All 4 Replies

i don't want it to result echo col1_id . col1_name (A john,A john, Ajohn) and col2 (bear,dog, cat)

You can use group_concat funtion

SELECT 
    col1_id,
    col1_name,

    GROUP_CONCAT(DISTINCT col2
        ORDER BY col2
        SEPARATOR ';')
FROM
    table

GROUP BY col1_id, col1_name

http://www.mysqltutorial.org/mysql-group_concat/

commented: I am gettind (A,john) but only one result for col2 (bear) +4

Altough it shows the results on inspector response it doen't print them Why? I want to show the results on GROUP__CONCAT

Hey, is the question still relevant?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.