sql select with column name like

前端 未结 8 1186
盖世英雄少女心
盖世英雄少女心 2020-12-02 15:46

I have a table with column names a1,a2...,b1.b2....

How can I select all those with column names like a%?

8条回答
  •  独厮守ぢ
    2020-12-02 16:22

    Blorgbeard had a great answer for SQL server. If you have a MySQL server like mine then the following will allow you to select the information from columns where the name is like some key phrase. You just have to substitute the table name, database name, and keyword.

    SET @columnnames = (SELECT concat("`",GROUP_CONCAT(`COLUMN_NAME` SEPARATOR "`, `"),"`") 
    FROM `INFORMATION_SCHEMA`.`COLUMNS` 
    WHERE `TABLE_SCHEMA`='your_database' 
        AND `TABLE_NAME`='your_table'
        AND COLUMN_NAME LIKE "%keyword%");
    
    SET @burrito = CONCAT("SELECT ",@columnnames," FROM your_table");
    
    PREPARE result FROM @burrito;
    EXECUTE result;
    

提交回复
热议问题