I have a table of mostly-static objectives and another to track users' completions of those objectives. They can associate the completion with one or more records entered in another table and/or a text note. I'd like to format all of this together into a single entry for displaying in a table (i.e. one row per objective).
Here's an example of what Completion
might look like:
ID userID objectiveID recordID text
1 4 8 500 NULL
2 4 8 NULL "Lorem ipsum..."
3 4 8 750 NULL
I've gotten this far:
SELECT objectiveID,
GROUP_CONCAT(recordID SEPARATOR ',') AS records,
GROUP_CONCAT(text SEPARATOR ',') AS text
FROM Completion AS c
GROUP BY objectiveID;
Which returns:
objectiveID records text
8 "500,750" "Lorem ipsum..."
What I'd actually like to display, however, is an attribute of the code being referenced by recordID
… Suppose that this is the Record
table:
ID userID codeID
500 4 1111
750 4 2222
And that this is the Code
table:
ID description
1111 dolor
2222 sit amet
My desired output would be:
objectiveID records text
8 "dolor, sit amet" "Lorem ipsum..."
What's the best approach to get the other values merged in?
Best Answer
You join
Completion
(columnsuserID
,recordID
) toRecord
(columnsuserID
,ID
)You join
Record
(columncodeID
) toCode
(columnID
)Here is the proposed query
Since GROUP_CONCAT's default separator is a comma, you could rewrite as