Domanda

How do i find similar column names from a database table?

for e.g.

a database table

1_1  1_2  1_3  5_6  67
    |    |    |    |   
    |    |    |    |

So, 1_1, 1_2, 1_3, 5_6 and 67 are the column names of a database table. And i would like to retrieve only the column names starts with 1 (1_1, 1_2 and 1_3). i tried the sql query but it dint work..

SELECT 1 LIKE '%1%' FROM sheet1;

It shows something of this short

1 LIKE '%1%'
         1
         1
È stato utile?

Soluzione

Documentation find here

SHOW COLUMNS FROM tbl_name FROM db_name
LIKE '1%'

To get the contents of the respective column:

SQL Fiddle

Altri suggerimenti

this may be useful to you

SELECT COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME='table' AND TABLE_SCHEMA='database_name'

information_schema is database containing meta data about all databases so when ever you want this kind of data you simply fire query on it

Try this:

SELECT GROUP_CONCAT(COLUMN_NAME) INTO @s 
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'database_name' AND TABLE_NAME = 'tableName' AND 
      COLUMN_NAME LIKE '1%';

SET @sql = CONCAT('SELECT ', @s, ' FROM tableName');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top