在 select 搜索中使用 information_schema.COLUMNS

Using information_schema.COLUMNS in select search

我正在尝试 select 除了一列之外的所有列,所以我正在使用这个:

select COLUMN_NAME 
from information_schema.COLUMNS 
where TABLE_NAME='report' and COLUMN_NAME != 'date';

它 returns 我得到了正确的结果,但是当我尝试使用它时:

select 
(
 select COLUMN_NAME 
 from information_schema.COLUMNS 
 where TABLE_NAME='report' and COLUMN_NAME != 'date'
) 
from report limit 1;

它给出了ERROR 1242 (21000): Subquery returns more than 1 row

的错误

我试图 group_concat 它但它 returns 只是列(查询):

select 
(
 select group_concat(COLUMN_NAME) 
 from information_schema.COLUMNS 
 where TABLE_NAME='report' and COLUMN_NAME != 'date'
) 
from risk_assessment.report limit 1;

| (select group_concat(COLUMN_NAME) from information_schema.COLUMNS where TABLE_NAME='report' and COLUMN_NAME != 'date')                                                            |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id,ref,creator,status,username,title,site,location,comment,id,status,type,chemical_name,description,title,creator,frequency,location,form,storage,comments,comments_tick,username |

我应该改变什么?

MariaDB 中没有相应的语法。您不能在单个语句中执行类似 (select * except x from table) 之类的操作。

不过,您可以通过几个步骤完成:

SET @sql = 
    CONCAT('SELECT ',
            (SELECT 
                    GROUP_CONCAT(COLUMN_NAME)
                FROM
                    INFORMATION_SCHEMA.COLUMNS
                WHERE
                    TABLE_SCHEMA = '<schema>'
                    AND TABLE_NAME = '<table>'
                        AND COLUMN_NAME NOT IN ('<column>')),
            ' FROM <table>');
PREPARE s FROM @sql;
EXECUTE s;