【发布时间】:2016-04-04 07:33:44
【问题描述】:
表结构:
CREATE TABLE IF NOT EXISTS mysql.`my_autoinc` (
`table_schema` VARCHAR(64) NOT NULL,
`table_name` VARCHAR(64) NOT NULL,
`auto_increment` INT(11) UNSIGNED NULL DEFAULT NULL, PRIMARY KEY (`table_schema`, `table_name`)
) ENGINE=InnoDB;
查询 1:
任何 DB 中名为 table1 或 table2 的所有表的列表。
REPLACE INTO mysql.`my_autoinc`
SELECT table_schema, table_name, NULL AS `auto_increment`
FROM information_schema.tables
WHERE table_name IN ("table1", "table2");
查询 1 可能会生成
table_schema | table_name | auto_increment
===============================================
client_1 | table1 | NULL
client_1 | table2 | NULL
client_2 | table1 | NULL
client_3 | table1 | NULL
查询 2: 查询字符串列表。
SELECT CONCAT(
'REPLACE INTO my_autoinc ',
'SELECT "',table_schema,'", "',table_name,'", MAX(Id) FROM ',
'('
'SELECT MAX(Id) AS Id FROM ', table_schema, '.', table_name,
' UNION ',
'SELECT MAX(Id) AS Id FROM ', table_schema, '_history.', table_name, '_history',
') t'
) AS 'queries'
FROM my_autoinc;
当查询2生成的查询列表被执行时。
table_schema | table_name | auto_increment
===============================================
client_1 | table1 | 99
client_1 | table2 | 60
client_2 | table1 | 299
client_3 | table1 | 399
我已经试过了: GROUP_CONCAT 但是字符串的串联长度超过了 1000。所以,无法执行该长度的查询。
更新:我不能multiple statements in a prepare。
需要解决方案:逐一执行查询 2 生成的查询。
【问题讨论】:
标签: mysql database auto-increment