0
votes

Is it possible to do something like this in MySql:

select *, CONCAT(schema_name,'.','my_table') as database_name from table_a a inner join table_b b on b.id = a.id inner join database_name c on a.id2 = c.id;

Basically, database_name's value is taken from the existing query and also it will be used in the same query.

Is it possible to do something like this without using stored proc? If not, is it possible to do this in Java without using loops?

1

1 Answers

0
votes

It's not possible to do this in a single query. It doesn't require a stored proc, though.

The identifiers in a query cannot be variable; MySQL needs to actually know the names of the objects (tables, columns, functions, et al.) when the SQL text is submitted; the identifiers can't vary at execution time.

This doesn't require a stored proc, but it does require two separate SELECT statements. One SELECT to retrieve the database identifiers. And the values from that can be used to formulate a string that contains SQL text for another SELECT to be submitted to the database.