How to generate a prepared MySQL query from a variable array? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to output MySQL query results in CSV format?How do I connect to a MySQL Database in Python?How to get a list of MySQL user accountsMySQL - UPDATE query based on SELECT QueryHow do I UPDATE from a SELECT in SQL Server?How to query MongoDB with “like”?What are the options for storing hierarchical data in a relational database?How to reset AUTO_INCREMENT in MySQL?SQL query return data from multiple tablesHow to import an SQL file using the command line in MySQL?
How to avoid introduction cliches
Seek and ye shall find
How do I check if a string is entirely made of the same substring?
Is accepting an invalid credit card number a security issue?
What is ls Largest Number Formed by only moving two sticks in 508?
What is the least dense liquid under normal conditions?
Why did C use the -> operator instead of reusing the . operator?
How can I wire a 9-position switch so that each position turns on one more LED than the one before?
Additive group of local rings
Expansion//Explosion and Siren Stormtamer
What is a 'Key' in computer science?
PIC mathematical operations weird problem
Retract an already submitted recommendation letter (written for an undergrad student)
What's parked in Mil Moscow helicopter plant?
How to open locks without disable device?
My bank got bought out, am I now going to have to start filing tax returns in a different state?
Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?
What is it called when you ride around on your front wheel?
What's the difference between using dependency injection with a container and using a service locator?
What to do with someone that cheated their way through university and a PhD program?
Are all CP/M-80 implementations binary compatible?
What was Apollo 13's "Little Jolt" after MECO?
Map material from china not allowed to leave the country
Second order approximation of the loss function (Deep learning book, 7.33)
How to generate a prepared MySQL query from a variable array?
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to output MySQL query results in CSV format?How do I connect to a MySQL Database in Python?How to get a list of MySQL user accountsMySQL - UPDATE query based on SELECT QueryHow do I UPDATE from a SELECT in SQL Server?How to query MongoDB with “like”?What are the options for storing hierarchical data in a relational database?How to reset AUTO_INCREMENT in MySQL?SQL query return data from multiple tablesHow to import an SQL file using the command line in MySQL?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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.
mysql sql
add a comment |
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.
mysql sql
add a comment |
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.
mysql sql
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.
mysql sql
mysql sql
asked Mar 22 at 15:05
nowoxnowox
6,968939110
6,968939110
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55302551%2fhow-to-generate-a-prepared-mysql-query-from-a-variable-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
add a comment |
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;
add a comment |
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;
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;
edited Mar 22 at 15:54
answered Mar 22 at 15:46
nowoxnowox
6,968939110
6,968939110
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55302551%2fhow-to-generate-a-prepared-mysql-query-from-a-variable-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown