Mysql 1054-未知列';客户';在';其中第'条;

Mysql 1054-未知列';客户';在';其中第'条;,mysql,sql,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

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

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]从@t1准备测试;[Err]1054-where子句中的未知列“cliente”


*为什么*

您的选择如下所示:

[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
SELECT count(0) FROM information_schema.COLUMNS
WHERE information_schema.COLUMNS.TABLE_NAME = cliente
当它看起来像这样时:

[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
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,'\' 
这是不对的

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

差不多

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

既然你知道名字

set @t1 = concat('SELECT count(0) FROM information_schema.COLUMNS WHERE 
information_schema.COLUMNS.TABLE_NAME =''',@tabela,''' AND 
information_schema.COLUMNS.COLUMN_NAME =''',@campo, '''');
您错过了引号。

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

set @t1 = concat('SELECT count(0) FROM information_schema.COLUMNS WHERE 
information_schema.COLUMNS.TABLE_NAME ='cliente' AND 
information_schema.COLUMNS.COLUMN_NAME ='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,'"');