Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select column names that match a criteria (MySQL)

Tags:

sql

mysql

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.

like image 540
iLikeBreakfast Avatar asked Oct 20 '25 04:10

iLikeBreakfast


1 Answers

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.

like image 141
fthiella Avatar answered Oct 22 '25 19:10

fthiella