【问题标题】:1054 - Unknown column 'cliente' in 'where clause'1054 - 'where 子句'中的未知列'cliente'
【发布时间】:2014-02-12 13:44:10
【问题描述】:
set @tabela='cliente';

set @campo='codigo';

set @t1 = concat('SELECT count(0) FROM information_schema.COLUMNS WHERE 
information_schema.COLUMNS.TABLE_NAME =',@tabela,' AND 
information_schema.COLUMNS.COLUMN_NAME =',@campo);

PREPARE teste FROM @t1;

execute teste;

================================================ ================

[SQL] set @tabela='cliente'; Affected rows: 0 Time: 0.003ms

[SQL] set @campo='codigo'; Affected rows: 0 Time: 0.001ms

[SQL] 
set @t1 = concat('SELECT count(0) FROM information_schema.COLUMNS WHERE information_schema.COLUMNS.TABLE_NAME =',@tabela,' AND information_schema.COLUMNS.COLUMN_NAME =',@campo);
Affected rows: 0
Time: 0.001ms

[SQL] PREPARE teste FROM @t1; [Err] 1054 - 'where 子句'中的未知列 'cliente'

*为什么? *

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    您的选择如下所示:

    SELECT count(0) FROM information_schema.COLUMNS
    WHERE information_schema.COLUMNS.TABLE_NAME = cliente
    

    什么时候应该是这样的:

    SELECT count(0) FROM information_schema.COLUMNS
    WHERE information_schema.COLUMNS.TABLE_NAME = 'cliente'
    

    添加必要的引号。我不知道确切的语法。应该是

    TABLE_NAME =''',@tabela,'''
    

    TABLE_NAME =\'',@tabela,'\' 
    

    编辑:我已经查过了。根据http://dev.mysql.com/doc/refman/5.0/en/string-literals.html,两个版本都是正确的。

    【讨论】:

      【解决方案2】:

      我认为您缺少该表名周围的 '

      你当前正在执行的是

      ... WHERE information_schema.COLUMNS.TABLE_NAME = cliente AND information_schema.COLUMNS.COLUMN_NAME = codigo;
      

      这是不正确的。

      如果您已经使用准备好的语句,则应该通过参数绑定来提供这些数据。

      类似

      set @t1 = SELECT count(0) FROM information_schema.COLUMNS WHERE 
          information_schema.COLUMNS.TABLE_NAME = ? AND 
          information_schema.COLUMNS.COLUMN_NAME = ?';
      PREPARE teste FROM @t1;
      EXECUTE teste USING @tablea, @campo;
      

      应该可以。

      【讨论】:

        【解决方案3】:

        问题是表名没有被引用:

        set @t1 = concat('SELECT count(0) FROM information_schema.COLUMNS WHERE 
        information_schema.COLUMNS.TABLE_NAME =''',@tabela,''' AND 
        information_schema.COLUMNS.COLUMN_NAME =''',@campo, '''');
        

        【讨论】:

          【解决方案4】:

          因为你知道名字...

          set @t1 = concat('SELECT count(0) FROM information_schema.COLUMNS WHERE 
          information_schema.COLUMNS.TABLE_NAME ='cliente' AND 
          information_schema.COLUMNS.COLUMN_NAME ='codigo');
          

          你错过了引号。

          【讨论】:

            【解决方案5】:

            请在有 varchar 或文本值的地方使用 ''',看起来像您的查询:

                declare @tabela varchar(200);
                declare @campo varchar(200);
                declare @t1 varchar(200);
            
            
                set @tabela='cliente';
                set @campo='codigo';
                set @t1 = concat('SELECT count(0) FROM information_schema.COLUMNS WHERE 
                information_schema.COLUMNS.TABLE_NAME =''',@tabela,''' AND 
                information_schema.COLUMNS.COLUMN_NAME =''',@campo,'''');
            
            
                PREPARE teste FROM @t1;
            
                execute teste;
            

            【讨论】:

              【解决方案6】:

              您没有将数据用引号括起来:

              sqlfiddle! http://sqlfiddle.com/#!2/0b900/15

              (这次是正确的链接)

              set @t1 = concat('SELECT count(0) FROM information_schema.COLUMNS WHERE 
              information_schema.COLUMNS.TABLE_NAME ="',@tabela,'" AND 
              information_schema.COLUMNS.COLUMN_NAME ="',@campo,'"');
              

              【讨论】:

                猜你喜欢
                • 2021-07-22
                • 2011-02-25
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-09-11
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多