1054 - 'where子句'中的未知列'cliente'

时间:2014-02-12 13:44:10

标签: mysql sql

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; [错误] 1054 - 未知专栏' cliente'在' where子句'

* 为什么? *

6 个答案:

答案 0 :(得分:0)

您的选择如下:

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,'\' 
编辑:我查了一下。根据{{​​3}},两个版本都是正确的。

答案 1 :(得分:0)

我认为你错过了该表名的'

您目前正在执行的是

... 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;

应该有用。

答案 2 :(得分:0)

问题是没有引用表名:

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

答案 3 :(得分:0)

因为你知道名字......

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

你错过了报价。

答案 4 :(得分:0)

请使用'''那里的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;

答案 5 :(得分:0)

您没有用引号括起数据:

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,'"');