sql select with column name like

前端 未结 8 1134
盖世英雄少女心
盖世英雄少女心 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 15:57
    SELECT * FROM SysColumns WHERE Name like 'a%'
    

    Will get you a list of columns, you will want to filter more to restrict it to your target table

    From there you can construct some ad-hoc sql

    0 讨论(0)
  • 2020-12-02 15:59

    This will get you the list

    select * from information_schema.columns 
    where table_name='table1' and column_name like 'a%'
    

    If you want to use that to construct a query, you could do something like this:

    declare @sql nvarchar(max)
    set @sql = 'select '
    select @sql = @sql + '[' + column_name +'],'
    from information_schema.columns 
    where table_name='table1' and column_name like 'a%'
    set @sql = left(@sql,len(@sql)-1) -- remove trailing comma
    set @sql = @sql + ' from table1'
    exec sp_executesql @sql
    

    Note that the above is written for SQL Server.

    0 讨论(0)
  • 2020-12-02 16:01

    You need to use view INFORMATION_SCHEMA.COLUMNS

    select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = my_table_name AND COLUMN_NAME like 'a%'
    

    TO inline rows you can use PIVOT and for execution EXEC() function.

    0 讨论(0)
  • 2020-12-02 16:15

    This will show you the table name and column name

    select table_name,column_name from information_schema.columns
    where column_name like '%breakfast%'
    
    0 讨论(0)
  • 2020-12-02 16:18

    Thank you @Blorgbeard for the genious idea.

    By the way Blorgbeard's query was not working for me so I edited it:

    DECLARE @Table_Name as VARCHAR(50) SET @Table_Name = 'MyTable'          -- put here you table name
    DECLARE @Column_Like as VARCHAR(20) SET @Column_Like = '%something%'    -- put here you element
    DECLARE @sql NVARCHAR(MAX) SET @sql = 'select '
    
    SELECT @sql = @sql + '[' + sys.columns.name + '],'
    FROM sys.columns 
    JOIN sys.tables ON sys.columns.object_id = tables.object_id
    WHERE sys.columns.name like @Column_Like
    and sys.tables.name = @Table_Name
    
    SET @sql = left(@sql,len(@sql)-1) -- remove trailing comma
    SET @sql = @sql + ' from ' + @Table_Name
    
    EXEC sp_executesql @sql
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题