我有这个查询。当我执行时,我得到了Query语句,但我想要这个查询的结果集。
我的查询是:
SELECT CONCAT('select * from ', table_name, ' union all ') AS CUSTOMQUERY
FROM information_schema.tables
WHERE table_name LIKE '%custom%'
我得到了这个结果
select * from custom_70006 union all
select * from custom_704306 union all
select * from custom_700436 union all
select * from custom_7000643 union all
select * from custom_7000634 union all
select * from custom_700046 union all
select * from custom_700063 union all
select * from custom_700062 union all
但我希望此特定列的结果集包含相应的数据,并且最后union all
应该删除。
请帮我查询相关问题。
答案 0 :(得分:1)
我认为这就是你要找的东西:
SET GLOBAL group_concat_max_len = 4294967295;
SELECT @query1 := GROUP_CONCAT(CONCAT('SELECT * FROM ', table_name) SEPARATOR
' UNION ALL ') AS CUSTOMQUERY
FROM INFORMATION_SCHEMA.tables
WHERE TABLE_SCHEMA = SCHEMA()
AND table_name LIKE '%custom%';
PREPARE stmt FROM @query1; EXECUTE stmt; DEALLOCATE PREPARE stmt;
或将您的查询修改为:
SELECT @query1 := CONCAT('select * from ', table_name, ' union all ') AS
CUSTOMQUERY
FROM information_schema.tables
WHERE table_name LIKE '%custom%'
从查询中删除最后一个'union all'
字符串:
SET @query1 = TRIM(TRAILING 'union all' FROM TRIM(@query1));
PS:group_concat_max_len的默认值仅为1024.因此您需要将其设置为更高的值,否则查询的输出将被剥离,您可能会遇到语法错误。