如何运行唯一列是表名的表,然后从每个表中进行选择

How do I run through a table where the only column is table names, and then select from each of those tables

提问人:Ron 提问时间:3/9/2023 最后编辑:Marcelo PacoRon 更新时间:3/9/2023 访问量:24

问:

首先,我创建一个临时表来保存数据库中包含名为 MasterRecID 的列的所有表

DROP TEMPORARY TABLE if exists TempTablesWithMasterRecID;
CREATE TEMPORARY TABLE TempTablesWithMasterRecID
(TableName VARCHAR(50));

INSERT INTO TempTablesWithMasterRecID
SELECT table_name FROM information_schema.`COLUMNS` InfSc
WHERE InfSc.COLUMN_NAME = 'MasterRecID';

Image showing 5 rows

现在,我想循环浏览此临时表,以查看每个具有该 MasterRecID 的表中有多少行,如下所示:

select count(*) from [[[TempTablesWithMasterRecID.TableName]]]

[[[ and ]]] 里面的部分是我需要解决的。谢谢。

任何可行的解决方案(带有示例代码)都是值得赞赏的。如果您认为在没有临时表的情况下可以做得更好,请同时提供该示例代码。

像这样的解决方案会很棒:

用户 15

ADPUPDATE 18

api_email_queue 27

合同 44

减价出售 482

等。

我尝试使用光标循环,但没有任何效果

MySQL 循环 变量

答: 暂无答案