0

I have a table that lists the names of the databases that have tables with the same schema. Each customer has a different database.

Is there a way to run a select on same table in multiple databases?

The first query I wrote was to get a list of databases concatenated the table name.

Select concat(database_name,'.','TableA') from dbnamelist

This results in output like

database1.TableA
database2.TableA

Now I want to select a value in the column type but across different tables.

Select type from database1.TableA,database2,TableA

Is it possible to do this in a single SQL statement?

MDCCL
8,5303 gold badges32 silver badges63 bronze badges
asked Oct 31, 2018 at 17:14
2
  • Is it possible to do this in a single SQL statement? No. Create stored procedure. Use prepared statement. Commented Oct 31, 2018 at 17:41
  • Thank you! Prepared statement is a good idea. Commented Oct 31, 2018 at 18:07

1 Answer 1

0
SELECT GROUP_CONCAT(CONCAT(table_schema, '.', tableA))
 FROM information_schema.tables
 WHERE table_name = 'tableA';

will generate the list you need.

Figure out what the query needs to look like (and I do not think your example is correct), and modify this SELECT to generate the desired query.

Then copy and paste it into mysql commandline tool to run it.

answered Nov 1, 2018 at 4:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.