0

我在 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

谢谢!

4

1 回答 1

0

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

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

于 2020-05-17T18:30:26.853 回答