I have a table with the following fields and example value:
coursecode BIO 101
a_code FA
b_code SP
c_code SU
d_code
e_code
I'm trying to populate a field in another table (using coursecode as the key) with a comma separated list of all the "code" fields that have values (i.e. "FA, SP, SU"). I am accomplishing this successfully with the following code, but it is messy and was wondering if there was a more elegant way to accomplish this (esp. without all the UNION statements):
SELECT coursecode, group_concat(codes)
FROM (
SELECT
coursecode,
a_code AS codes
FROM scbsupp
WHERE coursecode="BIO 317"
UNION
SELECT coursecode,
b_code AS codes
FROM scbsupp
WHERE coursecode="BIO 317"
UNION
SELECT
coursecode,
c_code AS codes
FROM scbsupp
WHERE coursecode="BIO 317"
UNION
SELECT coursecode,
d_code AS codes
FROM scbsupp
WHERE coursecode="BIO 317"
UNION
SELECT coursecode,
e_code AS codes
FROM scbsupp
WHERE coursecode="BIO 317"
) WHERE codes != "";
For my own knowledge I would like to know if there's a better way to do this, I appreciate any help!