如何从另一个表的结果中插入表名

问题描述 投票:0回答:1

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

表中的架构:enter image description here

我需要对此表进行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个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.