Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT only TEXT COLUMNs

Tags:

mysql

I want to select values of columns whose type is "text". Something like:

SELECT (SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'mytable' AND COLUMN_TYPE='text') FROM `mytable`

Is it possible? I have searched a lot but found nothing, for which I am little bit pessimistic.

[Ofcourse the issue in above statement is 2nd SELECT returns multiple rows]

#1242 - Subquery returns more than 1 row
like image 576
abdfahim Avatar asked Dec 05 '25 16:12

abdfahim


1 Answers

You need to use a lot of statements here aside from the regular query not to mentioned you need to get the columns from Information.Schema. You can use GROUP_CONCAT() function along with PREPARE and EXECUTE statements.

And it will probably look like this:

SET @col = NULL;
SET @query = NULL;

SELECT (
   SELECT GROUP_CONCAT(COLUMN_NAME) as ColumnNames
   FROM INFORMATION_SCHEMA.COLUMNS
   WHERE table_name = 'table_name' 
   AND data_type='text' )
INTO @col;

SET @query = CONCAT('SELECT ', @col, ' FROM table_name');

PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

See my Sql Fiddle Demo.

like image 66
Edper Avatar answered Dec 08 '25 09:12

Edper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!