在选择搜索中使用information_schema.COLUMNS

时间:2017-01-18 02:16:56

标签: mysql mariadb

我正在尝试选择除一个之外的所有列,所以我正在使用它:

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

它会返回正确的结果,但是当我尝试使用以下方法应用它时:

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但它只返回了列(查询):

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 |

我应该改变什么?

1 个答案:

答案 0 :(得分:0)

MariaDB中没有这种语法。您不能在单个语句中执行(select * from table 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;