in Education by
My goal is to merge tables from different schema into one single schema so I would like to execute this query: INSERT INTO inventory.maintenance SELECT * FROM ( SELECT NULL, 'A' as department_id, maintenance.* FROM a_inventory.maintenance UNION SELECT NULL, 'B' as department_id, maintenance.* FROM b_inventory.maintenance UNION SELECT NULL, 'E' as department_id, maintenance.* FROM e_inventory.maintenance UNION SELECT NULL, 'L' as department_id, maintenance.* FROM l_inventory.maintenance UNION SELECT NULL, 'M' as department_id, maintenance.* FROM m_inventory.maintenance ) AS tmp_maintenance I have to do this query for many different tables and all tables are not in all departments. Instead of writing each queries manually, I would like to add a bit of automation... SET @department = ('A', 'B', 'E', 'L', 'M'); SET @connection = 'inventory'; SET @table = 'maintenance'; /* MAGIC HERE to create @union */ SET @s = CONCAT( 'INSERT INTO ', @connection, '.', @table, 'SELECT * FROM (', @union, ') AS tmp'); PREPARE stm1 FROM @s; EXECUTE stm1; DEALLOCATE PREPARE stm1; I would like to create @union from @department. I have tried to use REPEAT, but it does not really work. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
One possible way to solve this problem. But you need to have a table departments somewhere from which you can extract the id assuming the id is a char: SET @department = 'A,B,E,L,M' COLLATE utf8mb4_unicode_ci; SET @connection = 'inventory'; SET @table = 'maintenance'; SET @union = ''; SELECT @union := CONCAT( @union, ' SELECT NULL, "', id , '", ', @table,'.* FROM ', id, '_', @connection, '.', @table, ' UNION ' ) FROM departments WHERE FIND_IN_SET(id, @department); SELECT @union := SUBSTRING(@union, 1, LENGTH(@union) - LENGTH('UNION ')); SET @s = CONCAT( 'INSERT INTO ', @connection, '.', @table, ' ' '( SELECT * FROM (', @union, ') AS tmp)'); SELECT @s; PREPARE stm1 FROM @s; EXECUTE stm1; DEALLOCATE PREPARE stm1;

Related questions

0 votes
    Why does this mysql query fail? UPDATE accounts SET motivation = IF(motivation+100...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    I want to connect to db on EC2 from my local machine, I am not able to do and have tried everything- I ... to connect to the database Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    How to enable inno-db support on installed instance of MySql? I have installed mysql-5.0.67-win32. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    In my database I have some records where I am sorting by a column that contains identical values: | ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I have a .sql file with an export from phpMyAdmin. I want to import it into a different server using the ... create a database first? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    How can I reset the AUTO_INCREMENT of a field? I want it to start counting from 1 again. Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I want to use python to connect to a MySQL database, how can I do that? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I have some data that looks like this: C:10 R:200 N/A E:3 N/A N:77 I'm trying to ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    If I have a table with results like this: 0001400OL 0578400OL 354085OL 48679OL and if I wanted to replace ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I'm trying to install MySQL for Visual Studio 1.2.8 and Connector/NET 8.0.13 via MySQL Installer ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I've come to this code, but from this i have to manually insert all columns and check it by each ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I am trying to delete several rows from a MySQL 5.0.45 database: delete from bundle_inclusions; The client ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 26, 2022 in Education by JackTerrance
0 votes
    We have a live MySQL database that is 99% INSERTs, around 100 per second. We want to archive the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I'm attempting to copy my MySQL database from an Amazon EC2 to an RDS: I successfully did a mysqldump of ... easy question. Thoughts? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I have set up a micro instance server on EC2 based MySQL server fails frequently and for the third time, ... run/mysqld/mysqld.pid Select the correct answer from above options...
asked Feb 3, 2022 in Education by JackTerrance
...