【问题标题】:Error when selecting colum names from table in MySQL information schema从 MySQL 信息架构中的表中选择列名时出错
【发布时间】:2016-12-21 22:49:51
【问题描述】:

我正在尝试从 MySQL 中的特定表中获取列名列表。我在跑步:

SELECT column_name
FROM information_schema.columns
WHERE table_name = `test 2.2`
    AND table_schema = test

数据库名为test,表名为test 2.2,其余语法看起来正确。但是我一直收到错误

错误代码:1054。“where 子句”中的未知列“test 2.2”

还有其他方法可以让我做我想做的事和/或如何解决这个错误?

【问题讨论】:

  • 您将值包装在 ` 中。你应该使用'

标签: mysql sql select information-schema


【解决方案1】:

对象名称(在本例中为表名)作为字符串字面量存储在信息架构中,因此应使用单引号 (') 进行查询:

SELECT column_name
FROM information_schema.columns
WHERE table_name = 'test 2.2' AND table_schema = 'test'
-- Here -----------^--------^--------------------^----^

【讨论】:

    【解决方案2】:

    字符串使用引号而不是反引号

    SELECT column_name
    FROM information_schema.columns
    WHERE table_name = 'test 2.2'
      AND table_schema = 'test'
    

    【讨论】:

      【解决方案3】:
      SELECT column_name
      FROM information_schema.columns
      WHERE table_name = "test 2.2"
          AND table_schema = "test"
      

      或者,你为什么不直接描述一下这张桌子。

      desc `test 2.2`;
      

      【讨论】:

      • 因为我正在尝试获取可以在程序中提取的列名列表。 SHOW COLUMNSDESCRIBE 都给了我Type, NULL, Default, etc. 我不想在我的程序中出现的信息。
      猜你喜欢
      • 2019-07-13
      • 2018-07-20
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多