【问题标题】:How to insert a table name from the result of another table如何从另一个表的结果中插入表名
【发布时间】:2020-05-05 13:12:27
【问题描述】:

我在 mysql 中有一个名为“tables”的表,该表包含所有表名和表 id 的名称。

表中的架构:

我需要对此表进行 INNER JOIN 并替换值 {table}{column_id}

    SELECT *
    FROM attributes_categories AS ac

    INNER JOIN attributes_codes AS aco ON aco.id_attribute_code = 
        ac.attributes_codes_id_attribute_code

    INNER JOIN {$table} AS b
    LEFT JOIN translations AS t ON t.id = b.{$column_id} 

我正在尝试这样的事情,但是,我无法替换“INNER JOIN {$table}”,因为我无法插入结果来代替表名:

    SELECT COLUMN_NAME
    FROM attributes_categories AS ac

    INNER JOIN attributes_codes AS aco ON aco.id_attribute_code = 
       ac.attributes_codes_id_attribute_code

    INNER JOIN tables AS tab ON tab.id_table = ac.tables_id_table

    INNER JOIN INFORMATION_SCHEMA.TABLES ON TABLE_NAME = tab.table

    INNER JOIN tab.table AS b
    LEFT JOIN translations AS t ON t.id = b.column_id

    WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='knskjhpumw1' 

结果是:

Mensagens do MySQL :

#1146 - Tabela 'tab.table' does not exist

谢谢!

【问题讨论】:

    标签: php mysql mariadb inner-join database-schema


    【解决方案1】:

    在存储过程中,您可以使用CONCAT()(等)构造一个查询并“执行”它。这样您就可以“轻松”插入动态表名(等)。

    另一种方法是在您的客户端代码中构造 SQL,例如 PHP 或 Java。

    【讨论】:

      猜你喜欢
      • 2014-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      相关资源
      最近更新 更多