I have the following structure in my DB:
id,col_a,col_b,col_c,etc...
Now, all the other columns except for id are of type boolean. Lets say for example that
col_a=1,
col_b=0,
col_c=1
I am looking for a way to return the names of the columns where the column is true (=1), so in this example the return should look something like col_a,col_c
There will be a dynamic number of columns, seeing as the table is altered often to add new columns and delete old ones.
The function I have thus far looks like this - it is the function that is supposed to return that string of column names...
DROP FUNCTION fn_access;
DELIMITER //;
CREATE FUNCTION fn_access (myid INT) RETURNS varchar(800)
DETERMINISTIC
BEGIN
DECLARE ret_val VARCHAR(800);
DECLARE col_name VARCHAR(255);
DECLARE i INT;
DECLARE num_rows INT;
DECLARE col_names CURSOR FOR
SELECT column_name
FROM information_schema.columns
WHERE `table_name` = 'access' AND `table_schema` = 'some_db' AND `column_name` <> 'id'
ORDER BY ordinal_position;
SELECT FOUND_ROWS() into num_rows;
SET i = 1;
the_loop: LOOP
IF i > num_rows THEN
CLOSE col_names;
LEAVE the_loop;
END IF;
FETCH col_names
INTO col_name;
SET ret_val = CONCAT(',' col_name);
SET i = i + 1;
END LOOP the_loop;
SELECT * FROM access WHERE id = @myid;
RETURN ret_val;
END
//
Is there any way to do this using straight SQL? I am using MySQL.
If I understand your question correctly, maybe you need something like this:
SELECT 'col_a' col
FROM yourtable
WHERE col_a
UNION
SELECT 'col_b'
FROM yourtable
WHERE col_b
UNION
SELECT 'col_c'
FROM yourtable
WHERE col_c
...
this will return all columns in your table that have at least one row where they are true.
Or maybe this:
SELECT
id,
CONCAT_WS(', ',
CASE WHEN col_a THEN 'col_a' END,
CASE WHEN col_b THEN 'col_b' END,
CASE WHEN col_c THEN 'col_c' END) cols
FROM
yourtable
that will return rows in this format:
| ID | COLS |
----------------------------
| 1 | col_a, col_c |
| 2 | col_a, col_b, col_c |
| 3 | |
| 4 | col_c |
...
Please see fiddle here. And if you need to do it dynamically, you could use this prepared statement:
SELECT
CONCAT(
'SELECT id, CONCAT_WS(\', \',',
GROUP_CONCAT(
CONCAT('CASE WHEN ',
`COLUMN_NAME`,
' THEN \'',
`COLUMN_NAME`,
'\' END')),
') cols FROM yourtable'
)
FROM
`INFORMATION_SCHEMA`.`COLUMNS`
WHERE
`TABLE_NAME`='yourtable'
AND COLUMN_NAME!='id'
INTO @sql;
PREPARE stmt FROM @sql;
EXECUTE stmt;
Fiddle here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With