-2

I'm struggling with a query I have to make for a program:

This is the structure of my table (example column names):

One ID and name can have multiple Coefficients, like:

ID-Name-Coefficient
1-NameHere-0.5
1-NameHere-0.6
1-NameHere-0.7

This is how I would like my query result to look like:

1-NameHere-0.5-0.6-0.7

So all the duplicate coefficnents, I want in seperate columns, but all in one row.

What would be the best way to achieve this query?

2
  • This is called a pivot. MySQL doesn't have built in functionality for this. Without any research effort, I don't think anyone will want to write the code for you. Commented Apr 18, 2013 at 16:40
  • if the number of coefficients are known this can be accomplished with inner joins based on the known quantity. If they are not limited, you're looking at some dynamic SQL. See: stackoverflow.com/questions/4078983/… for more info Commented Apr 18, 2013 at 16:43

1 Answer 1

2

You can't have variable column count in SQL, but you can concatenate the values in one column:

SELECT  id, name, GROUP_CONCAT(coefficient)
FROM    mytable
GROUP BY
        id, name

This will return something like

0.5,0.6,0.7

in the third column which you can later parse on the client.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.